standalone executable and write to a txt file

5 次查看(过去 30 天)
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 个评论
Elijah
Elijah 2024-7-15
编辑:Elijah 2024-7-15
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
Elijah 2024-7-16
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')

请先登录,再进行评论。

回答(3 个)

Walter Roberson
Walter Roberson 2017-6-6
移动:Image Analyst 2024-8-1
Are you creating a fully-qualified file name to write in to? By default the output would go to ctfroot()

Harsh
Harsh 2024-8-1
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!

Image Analyst
Image Analyst 2024-8-1
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.

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by