How to use 'fprintf' to show the output in a txt file and save it instead of command window in Matlab? - matlab

I've been trying to make the output shown in the text file instead of the command window. Im blur right now as i already look at a lot of example but it always show the error on fprintf. I try to edit the code of fprintf(fid,'%s\n',word);%Write 'word' in text file (upper) in one of the Matlab example which is Automatically Detect and Recognize Text in Natural Images.
This is the link of the code.
https://www.mathworks.com/help/vision/examples/automatically-detect-and-recognize-text-in-natural-images.html?s_tid=srchtitle
Basically the above link display the output on the command window. But, i need it to be on the txt file.
Im really new to this, i want to know what code do i need to put, how and where should i put the fprintf to make the output shown on the text file and not on the command window.
Also, can i save the text file after that? do i to put any additional code?
I really need your help. Thank u in advance!

It seems you're looking for the fopen() method. It takes two parameters, the first being the name of the file you'd like to write to, and the second being the mode. If the file specified does not exist in the root directory, it will be created on execution.
fileID = fopen('exp.txt','w');
fprintf(fileID, fid,'%s\n', word);
fclose(fileID); % Make sure to always close the stream after finishing
More on fopen() here

Related

Matlab s2p file

I'm trying to build a s2p file in simulink using the block "To File" but gives me an odd file with random characters like
fs%($&%%(&
Which looks like when I try to open a jpeg file with block note.
I'm trying a simple RF layout with a single resistive divider and the input and output ports like in figure
anyone knows what is happening here?
The To File block writes a .mat (binary) data file. Opening one as a text file is always going to show odd random characters
To see what's in the file you need to use load to load the saved signal(s) into the MATLAB Workspace.

Opening a textfile in MatLab

I have written a simple textfile using notepad, and I placed the text file in the same folder as a matlab script I want to run. I pretty much want to make a script so that when the user clicks run a pop-up of my text file will come up purely within matlab.(I will later include separate figures, graphs to pop up at the same time.)
I tried researching, and I found fopen. But when I do fopen('FileName.txt','r'); nothing happens?
I am not sure of what you are looking for, but I guessed that you need to open a file and read its content using Matlab.
fid = fopen('FileName.txt','r'); % generate a file identifier
if fid < 0
error 'Unable to open file ... check file location'
else
while ~feof(fid) % while there is a line to read
disp(fgets(fid))
end
end
fclose(fid);

fprintf after opening file using fopen does not work in matlab

May be my tired eyes are ignoring something trivial. But I am not able to get the following code work in matlab. I need to open an existing file and write text to it.
filename='E:\\data_bak\\test.txt';
fileId=fopen(filename,'r+');
if(fileId~=-1)
myline=fgetl(fileId);
nbytes=fprintf(fileId,'%s\n','testdata')
fclose(fileId);
end
I am using matlab 7.9.0 on windows 7. I have tried rt+ for permissions in fopen. Also, fileId is not equal to zero as I am able to read the line in the variable myline. In addition, fprintf('%s\n','testdata') successfully prints testdata on the matlab prompt.
The value assigned to nbytes is 9. So it does appear it has written to file but when I open the file in text editor, I am not able to find text 'testdata'.

Editing a Text File from Matlab

I'm still getting used to Matlab, and not sure if this is possible using Matlab or not, but it's just something that popped into my head that I thought could be interesting.
Is there any way to edit the contents of a text file in Matlab?
Moreover, is there any way to edit specific parts of the text file without altering the rest?
To elaborate, let's say I had a text file that was several lines long. For instance:
This is a hypothetical text file.
The cat chased a mouse.
The mouse ran into a hole.
The cat tried to paw at the mouse.
The mouse waited in the hole until the cat got bored.
The mouse came back out when the cat left.
Is there any way to use Matlab to exclusively edit, say, line 6 and change it from "The mouse waited in the hole until the cat got bored" to "The mouse fell asleep and the cat got bored", without having to change the rest of the file?
I know of several methods to read and display contents of text files using Matlab, but I'm not sure if there's any way to actually edit the text files in Matlab.
Thanks!
As far as I know, you will always have to read the file line by line (for instance into a cell-array) and edit it as you need. After that, you write a new file or overwrite the old one.
Of course, you can encapsulate this procedure and then call you own function like
manipulateFile(lineNumber, newLineText)
Some commands that may come in handy are fopen, fscanf, textread, fprintf, and fclose.

Write a figure to a file automatically in MATLAB

Does anyone know if it's possible to automatically write a figure out to a .eps file in MATLAB?
I'm running a script that produces a large number of graphs, and it'd be nice if I didn't have to manually save each one!
print function does that:
Print figure or save to specific file format...
print(filename,formattype) saves the current figure to a file using the specified file format, such as print('BarPlot','-dpng'). If the file name does not include an extension, then print appends the appropriate one.
print(filename,formattype,formatoptions) specifies additional options that are available for some formats.
print prints the current figure to the default printer...
print or saveas will do the trick.
saveas(fig_handle, 'filename','eps')
print('-deps',fig_handle)
print -deps 1
If you want to specify the output file name, you're better off using saveas.
This was answered in this other question, using the PRINT command. Although that question dealt with making .tiff images, it should be straightforward to modify the code given in those answers to write a .eps.
Suppose, you are generating N numbers of figures in a loop, then you should try the command line:
saveas(gca,sprintf('Figure%02d.pdf',N )); it produces N figures Figure1.pdf - FigureN.pdf
saveas(gca,sprintf('Figure%02d.eps',N )); it produces N figures Figure1.eps - FigureN.eps
in place of gca one can use gcf also. First command line is a better solution.
Hope this will solve your issue.