Matlab: Printing literally using fprintf - matlab

I am writing a Matlab function that does some file manipulation as follows. I take the input file, read it line by line and write it to an output file. If the line contains a keyword, I do some further processing before writing the output. My problem is this: If the input line contains an escape character, it messes up the fprintf that I use to write the output. For example, if the input line contains a % sign, the rest of the line does not show up in the output. So, my question is: is there a way to force fprintf to ignore all escape sequences and print the literal string? Thanks in advance for the help.
Sample code below:
fptr_read = fopen('read_file.txt','r'); fptr_write = fopen('write_file.txt','w');
while(~feof(fptr_read))
current_line = fgetl(fptr_read);
fprintf(fptr_write,current_line);
end
If current_line looks like 'Gain is 5% larger', it will get written as 'Gain is 5'. I want the line reproduced verbatim without manually having to check for presence of escape characters.

I had a similar problem once, and solved it like this:
fprintf(fp,'%s',stringWithEscapesIWant)

Related

MATLAB simultaneous read and write the same file

I want to read and write the same file simultaneously. Here is a simplified code:
clc;
close all;
clearvars;
fd = fopen ('abcd.txt','r+'); %opening file abcd.txt given below
while ~feof(fd)
nline = fgetl(fd);
find1 = strfind(nline,'abcd'); %searching for matching string
chk1 = isempty(find1);
if(chk1==0)
write = '0000'; %in this case, matching pattern found
% then replace that line by 0000
fprintf(fd,'%s \n',write);
else
continue;
end
end
File abcd.txt
abcde
abcd23
abcd2
abcd355
abcd65
I want to find text abcd in string of each line and replace the entire line by 0000. However, there is no change in the text file abcd.txt. The program doesn't write anything in the text file.
Someone can say read each line and write a separate text file line by line. However, there is a problem in this approach. In the original problem, instead of finding matching text `abcd, there is array of string with thousands of elements. In that case, I want to read the file, parse the file for find matching string, replace string as per condition, go to next iteration to search next matching string and so on. So in this approach, line by line reading original file and simultaneously writing another file does not work.
Another approach can be reading the entire file in memory, replacing the string and iterate. But I am not very sure how will it work. Another issue is memory usage.
Any comments?
What you are attempting to do is not possible in a efficient way. Replacing abcde with 0000, which should be done for the first line, would require all remaining text forward because you remove one char.
Instead solve this reading one file and write to a second, then remove the original file and rename the new one.

Removing quotes that seem to be nonexistent in MatLab

I have an application that translates the .csv files I currently have into the correct format I need. But, the files I that I do have seem to have '"' double quotes around them, as seen in this image, which will not work with the program. As a result, I'm using this command to remove them:
for m = 1:currentsize(1)
for n = 1:currentsize(2)
replacement{m,n} = strrep(current{m,n}, '"', '');
end
end
I'm not entirely sure that this works though, as it spits this back at me as it runs:
Warning: Inputs must be character arrays or cell arrays of strings.
When I open the file in matlab, it seems to only have the single quotes around it, which is normal for any other file. However, when I open it in notepad++, it seems to have '"' double quotes around everything. Is there a way to remove these double quotes in any other way? My code doesn't seem to do anything as seen here:
After using xlswrite to write the replacement cell-array to a .csv file, one appears corrupted. Any idea why?
So, my questions are:
Is there any way to remove the quotes in a more efficient manner or without rewriting to a csv?
and
What exactly is causing the corruption in the xlswrite function? The variable replacement seems perfectly normal.
Thanks in advance!
Regarding the "corrupted" file. That's not a corrupted file, that's an xls file (not xlsx). You could verify this opening the text file in a hex editor to compare the signature. This happens when you chose no file extension or ask excel to write a file which can't be encoded into csv. I assume it's some character which causes the problems, try to isolate the critical line writing only parts of the cell.
Regarding your warning, not having the actual input I could only guess what's wrong. Again, I can only give some advices to debug the problem yourself. Run this code:
lastwarn('')
for m = 1:currentsize(1)
for n = 1:currentsize(2)
replacement{m,n} = strrep(current{m,n}, '"', '');
if ~isempty(lastwarn)
keyboard
lastwarn('')
end
end
end
This will launch the debugger when a warning is raised, allowing you to check the content of current{m,n}
It is mentionned in the documentation of strrep that :
The strrep function does not find empty strings for replacement. That is, when origStr and oldSubstr both contain the empty string (''), strrep does not replace '' with the contents of newSubstr.
You can use this user function substr like this :
for m = 1:currentsize(1)
for n = 1:currentsize(2)
replacement{m,n} = substr(current{m,n}, 2, -1);
end
end
Please use xlswrite function like this :
[status,message] = xlswrite(___)
and check the status if it is zero, that means the function did not succeed and an error message is generated in message as string.

MATLAB Saving Figure Invalid Filename Error

I am writing a program to plot graphs in a loop and I want to save each graph that comes out as a .jpg file with a variation of the file name. Here is my code for saving the graphs:
filename = strcat('WI_Pollutants_', D(i,6), '_200706_O3');
saveas(gcf, filename, 'jpg');
The saved file should come out as the following with D(i,6) changing each iteration of the loop.
WI_Pollutants_003-0010_200706_O3.jpg
However, I'm running an error: (Maybe it has to due with saveas wanting a string only?)
Error using saveas (line 81)
Invalid filename.
saveas only accepts characters as the filename. But when filename was created, strcat made it a cell array. Therefore, the filename needs to be converted to a character array.
filename = char(strcat('WI_Pollutants_', D(i,6), '_200706_O3'));
saveas(gcf, filename, 'jpg');
This solves the problem.
I think your D{i,6} is ending up wrapped up as an array, from this line:
D{i,6} = [num2str(D{i,6}) '-' num2str(D{i,7})];
To solve this, just removing the []'s
D{i,6} = num2str(D{i,6}) '-' num2str(D{i,7});
I think what happened is your D{i,6}=['someString'], and concatinating added in the []'s that werent' desired.
As a check, if something like this happens again, just fprintf(filename) right before use, and look at what comes out. I suspect you'll find the issue there. You can always remove the print statement later on.

Formatting fprintf

I have a bit of Matlab code and I am trying to export some string and create a tab-delimitted text file from it. I think fprintf performs similarly in C (if not please edit my tag). I believe my issue is with my format string. Basically I have 7 strings that I want separated by tabs and then a newline character. please note that "fid" is a full path. I am looping this in a for loop so lines are being appended each pass and the file is built.
ImgData = strcat(ImgData, fid, '\t', imgNumber, '\t', N_std,'\t',S,'\t',N,'\t',SNR,'\t',SNR_dB,'\n');
DataOut = fopen(strcat('Image_F', folderNumber, '_Data.txt'), 'w');
fprintf(DataOut,'%s\t %s\t %s\t %s\t %s\t %s\t %s\n',ImgData);
You may be curious on how this exports. This formats like
fid\tI#\tN_std\tS\tN\tSNR\tSNR_dB\n
in the txt file. As you can tell this isn't tab-delimitted which my major issue. I am having some trouble with the format string. Does anyone know how to reformat it so it prints the tabs and newline?
You're creating a string in ImgData that you then pass as input to fprintf. This reads ImgData into the first %s of the format string, and then adds at least one tab at the end.
What you should do instead is write something like:
`fprintf(DataOut,'%s\t%i\n',imgName,imgNumber)`
which assumes that imgName is a string and imgNumber an integer number. Note that I pass two placeholders (with the %-sign) and two input variables to fprintf.
Use %6.2f print floating point numbers with a total of 6 characters, including 2 after the comma, for SNR, for example.
For easier development, you can drop the first input argument to fprintf, in which case it will print to command line.

MATLAB: How do you insert a line of text at the beginning of a file?

I have a file full of ascii data. How would I append a string to the first line of the file? I cannot find that sort of functionality using fopen (it seems to only append at the end and nothing else.)
The following is a pure MATLAB solution:
% write first line
dlmwrite('output.txt', 'string 1st line', 'delimiter', '')
% append rest of file
dlmwrite('output.txt', fileread('input.txt'), '-append', 'delimiter', '')
% overwrite on original file
movefile('output.txt', 'input.txt')
Option 1:
I would suggest calling some system commands from within MATLAB. One possibility on Windows is to write your new line of text to its own file and then use the DOS for command to concatenate the two files. Here's what the call would look like in MATLAB:
!for %f in ("file1.txt", "file2.txt") do type "%f" >> "new.txt"
I used the ! (bang) operator to invoke the command from within MATLAB. The command above sequentially pipes the contents of "file1.txt" and "file2.txt" to the file "new.txt". Keep in mind that you will probably have to end the first file with a new line character to get things to append correctly.
Another alternative to the above command would be:
!for %f in ("file2.txt") do type "%f" >> "file1.txt"
which appends the contents of "file2.txt" to "file1.txt", resulting in "file1.txt" containing the concatenated text instead of creating a new file.
If you have your file names in strings, you can create the command as a string and use the SYSTEM command instead of the ! operator. For example:
a = 'file1.txt';
b = 'file2.txt';
system(['for %f in ("' b '") do type "%f" >> "' a '"']);
Option 2:
One MATLAB only solution, in addition to Amro's, is:
dlmwrite('file.txt',['first line' 13 10 fileread('file.txt')],'delimiter','');
This uses FILEREAD to read the text file contents into a string, concatenates the new line you want to add (along with the ASCII codes for a carriage return and a line feed/new line), then overwrites the original file using DLMWRITE.
I get the feeling Option #1 might perform faster than this pure MATLAB solution for huge text files, but I don't know that for sure. ;)
How about using the frewind(fid) function to take the pointer to the beginning of the file?
I had a similar requirement and tried frewind() followed by the necessary fprintf() statement.
But, warning: It will overwrite on whichever is the 1st line. Since in my case, I was the one writing the file, I put a dummy data at the starting of the file and then at the end, let that be overwritten after the operations specified above.
BTW, even I am facing one problem with this solution, that, depending on the length(/size) of the dummy data and actual data, the program either leaves part of the dummy data on the same line, or bring my new data to the 2nd line..
Any tip in this regards is highly appreciated.