standalone executable and write to a txt file
3 views (last 30 days)
Show older comments
Hi everybody,
I have one program that runs perfectly in matlab. The program when receive one input, write in report.txt.
With standalone Executable i maked a exe. but when i running the exe, and give a input of program, the program doesn't write to the report.txt.
Anyone knows how to solve this problem?
Thank you in advance.
2 Comments
Elijah
on 15 Jul 2024
Edited: Elijah
on 15 Jul 2024
I am having the same issue. I created a very simple program to test. It opens a new file and writes to that new file. When trying to convert to the executable through the Matlab Coder, I first have to create a C file. During that proccess, it works, but when converting to the excutable with the additional C file, the executable does not work. I am able to change to C file to include printf and It is able to use printf to display in the Matlab's command window, and the function returns 0, which is the default when it is done running code.
function Test_Write()
device = fopen('any location',"w+"); % insert location
fwrite(device_write, 'This Works', "char")
end
With the code above, I follow the steps on this page. And after removing 'This Works' from the C file test, nothing is printed on the file when the executable is run.
Elijah
on 16 Jul 2024
I was able to get it working with this code. It will create a folder with the executable inside. To call it use system. The name does not have to have .exe at the end. For some reason I cannot get Matlab Coder to work.
buildResults = compiler.build.standaloneApplication('your_file.m')
system('your_file.exe')
Answers (3)
Walter Roberson
on 6 Jun 2017
Moved: Image Analyst
on 1 Aug 2024
Are you creating a fully-qualified file name to write in to? By default the output would go to ctfroot()
0 Comments
Harsh
on 1 Aug 2024
It seems the goal is to create a standalone application that generates a text file based on given input.
If the text file is not appearing in the expected location, try saving the file to a specified location. Here's an example of how to do this:
- As a background, a button was added to a sample app, and the following script was included in its callback function.
filePath = 'path\to\your\file';
% Open the file for writing
fileID = fopen(filePath, 'w');
% Check if the file opened successfully
if fileID == - 1
errormsg( 'Failed to open the file.');
end
% Define the text to write
text = 'This is a sample text. ';
% Write the text to the file
fprintf(fileID, '%s\n', text);
% Close the file
fclose( fileID);
- If specifying a static path is not preferred, allow the user to choose the path using the “uigetdir” function. Here’s how:
filename = uigetdir('C:\', 'Select a folder');
% Open the file for writing
fileID = fopen([filename '\example.txt'], 'w');
% Check if the file opened successfully
if fileID == - 1
errormsg( 'Failed to open the file.');
end
% Define the text to write
text = 'This is a sample text.';
% Write the text to the file
fprintf(fileID, '%s\n', text);
% Close the file
fclose(fileID);
- A UI dialog like the following will appear.
For more information about the “uigetdir” function, refer to the MathWorks documentation:
- https://www.mathworks.com/help/matlab/ref/uigetdir.html
Hope this helps, thanks!
0 Comments
Image Analyst
on 1 Aug 2024
Like the others said, give the full file name. This means the drive, the folder, the base file name, and the extension. Use fullfile. That way you know exactly where it will go. Or let the user choose with uiputfile.
For more info, see the FAQ: https://matlab.fandom.com/wiki/FAQ#Why_can't_my_standalone_compiled_executable_find_my_files?
0 Comments
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!