I have a very long script with a lot of plot functions and related legend functions but these legend functions use 1, 2, 3 or 4 to locate legends on corners (as it was in older versions). However now I need to change them with name/value pairs. To do so, I wrote down a script to convert them. I want to copy code into text file and converted code will be on another text file so I can recopy it to my .m files. Here my code is;
fid=fopen('drawFigure.txt','r');
fid2=fopen('drawFigure2.txt','w');
codeLine=fgetl(fid);
while(codeLine ~= -1)
legAv=strfind(codeLine,'legend');
if (~isempty(legAv))
loc3=strfind(codeLine,'3');
loc2=strfind(codeLine,'2');
if(~isempty(loc3))
loc=loc3;
elseif(~isempty(loc2))
loc=loc2;
end
%here I check the next indice of codeLine to make sure I will change location factor
if(codeLine(loc+1)==')')
codeLine(loc)=[];
str1=codeLine(1:loc-1);
str2=codeLine(loc:length(codeLine));
mystr="'location','northeast'";
codeLine=strcat(str1,mystr,str2);
endif
endif
fprintf(fid2,'%s\n',codeLine);
codeLine=fgetl(fid);
endwhile
fclose(fid2);
fclose(fid);`
In this code, If I wrote \t, it would be working but \n is not working. Also, tried to get help from a software engineer friend but he couldn’t figure out the reason.
From your information, I am guessing that you are opening your file using Windows notepad. If this is the case, newlines (\n) are not shown unless preceeded by a cariage return (\r). This means that you should change:
fprintf(fid2,'%s\n',codeLine);
to
fprintf(fid2,'%s\r\n',codeLine);
or alternatively, use a basic text editor with more support such as Notepad++ for Windows.
Related
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
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'.
I want to create a data file similar like this:
I developed a script file to do this like that:
fid = fopen('output.dat','w');
writetext = 'zone i=401, j=961';
fwrite(fid,writetext);
dlmwrite('output.dat',sort2,'delimiter','\t','precision','%.8f');
fclose(fid);
However, when I run this code, the text part of the file, "zone i=401, j=961" disappears as seen in the image below:
How can I keep the text part of the data file on the top of my file?
There are two problems with your code:
dlmwrite by default overwrites the whole file. Use '-append' option to avoid that (as noted by #excaza in comments).
You need a new line character after the title. For that, use fprintf and add n or \r\n after your title. \n is used in general to start new line, but \r\n may be needed in Windows.
With these changes, the code is as follows. Changed lines are third and fourth.
fid = fopen('output.dat','w');
writetext = 'zone i=401, j=961';
fprintf(fid, '%s\r\n', writetext);
dlmwrite('output.dat',sort2,'-append','delimiter','\t','precision','%.8f');
fclose(fid);
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.
I am wondering if I have some code with line numbers embedded,
1 int a;
2 MyC b;
3 YourC c;
etc., and then I copy them and try to paste them in Eclipse, how to get rid of these line numbers to make the source code valid? Is there any convenient way, or a short-cut key?
Thank you.
Simply use the Alt+Shift+A (Eclipse 3.5 M5 and above) shortcut to toggle block selection mode. Then select the column with line numbers and delete it!
To make it easier you could setup a macro, but for that you need additional plug-in. I'm not aware of how to do it even easier.
Try this link. This is a dynamic online tool, where it is very easy to just copy paste code and get code without line numbers:
http://remove-line-numbers.ruurtjan.com/
You could use some script to do the work. For instance, using sed
I removed line numbers by find and replace with regular expression option.
Replacing regular expression \d+\s\s with empty string where \d+ means any combination of numbers and \s is actually a space (This is to avoid any numbers present in the code).
Best way is use SED command. Here you can specify as many as digit you want to replace.
in below example open copied code in VI editor and assuming its containing upto 1000 lines.
:%s/^[0-9][0-9|10-99|100-999]//g
if you want to use more lines then put one more or condition.