Why does fprintf command display >> in MATLAB? - matlab

Here is a sample of a random script in MATLAB.
prompt = 'Please enter a lowercase x: ';
str = input(prompt, 's');
if str == 'x'
else
fprintf('Error, you did not enter a lowercase x.')
end
This always display what I have in the fprintf command with a >> at the end of it in the command window. For example, in this random context it would display ...
Error, you did not enter a lowercase x.>>
Simple question, yet I'm new to MATLAB. Why do I get a >> at the end of every fprintf command? Can't seem to figure it out.

You did not specify a line-break in your string, so fprintf pushes the text to the command windows and spawns another input prompt (the >>) directly after the text. Add a line break meta-character to the string (\n) to fix the issue:
fprintf('Error, you did not enter a lowercase x.\n')
Also, if your goal is to issue an error, you should use the error function. It halts execution of the code and colors the message red like other MATLAB errors.

Here the fprintf simply displays the text and returns to command console.
Use newline '\n' character,
fprintf('Error, you did not enter a lowercase x.\n');
% ~~~
to come back atfer newline with >> prompt

Related

Can we use a command to append any text to the current edited file or after prompt

1) suppose I use the 'edit' command only an unnamed file 'untitled'.
can I use any command to append any text to the file 'untitled'?
2) Is there a command that can write a command after the prompt symbol >> w/o pressing enter. Like
>> doit
This is different from printf. printf('doit') does the following
doit
>>
To add text in a file 'untitled.m' using a function doit()
function doit()
fid = fopen('untitled.m','w'); % open file with write access
fprintf(fid,'%s','This text will appear in untitled.m'); % write some text as string (%s)
fclose(fid); % close the file (important!)

How to save the command history in a text file in MATLAB

I want to save the value of a variable from MATLAB command history in a text. I am trying the command:
Save([d:/work/abc.txt], 'z1', '-ASCII');
An error appears
Error: input charecter is not valid in MATLAB environment or expression.
What you are missing are the quotes within the brackets for denoting string.
['string']
You should use save (with lower case for "s").
Also the filename should be defined as a string: enclose it withi two '; also you do not need the [] unless, for example, you want to build a string using a variable and / or any function to create part of the filename (e. g.
['d:/work/abc_' num2str(k) '.txt']
assuming k value is 3) to get d:/work/abc_3.txt
Try change your code to:
save(['d:/work/abc.txt'], 'z1', '-ASCII');
Hope this helps.
Qapla

How do I export a matrix in MATLAB?

I'm trying to export a matrix f that is double. My data in f are real numbers in three columns. I want a txt file as an output with the columns separated by tabs. However, when I try the dlmwrite function, just the first column appears as output.
for k = 1:10
f = [idx', firsttime', sectime'];
filename = strcat(('/User/Detection_rerun/AF_TIMIT/1_state/mergedlabels_train/'),(files_train{k,1}),'.lab');
dlmwrite(filename,f,'\t') ;
end
When I use dlmwrite(filename,f,'\t','newline','pc') ; I keep getting an error Invalid attribute tag: \t . I even tried 'tab' instead of '\t' but a similar error appears. Please let me know if you have any suggestions. thank you
This is because you are not calling dlmwrite properly. To specify the delimiter, you must use the delimiter flag, followed by the specific delimiter you want. In your case, you use \t. In other words, you need to do this:
for k = 1:10
f = [idx', firsttime', sectime'];
filename = strcat(('/User/Detection_rerun/AF_TIMIT/1_state/mergedlabels_train/'),(files_train{k,1}),'.lab');
dlmwrite(filename,f,'delimiter','\t') ;
end
BTW, you are using the newline flag with pc, meaning that you are specifying carriage returns that are recognized by a PC. I suggest you leave this out and allow MATLAB to automatically infer this. Only force the newline characters if you know what you're doing.
FWIW, the MATLAB documentation is pretty clear about delimiters and other quirks about the function: http://www.mathworks.com/help/matlab/ref/dlmwrite.html

Matlab: Printing literally using fprintf

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)

Matlab fopen command responds to string but not variable equaling same string

I'm wondering if anyone can shed some light on the following issue with Matlab fopen command:
>> [stat myjob] = unix('echo $PBS_NODEFILE'); % gets PBS file name with allocated nodes
>> myjob
myjob =
/opt/torque/aux//66058.crunch.local
>> fid = fopen('/opt/torque/aux//66058.crunch.local')
fid =
3
>> fgetl(fid)
ans =
compute-9-2
>> fclose(fid);
I need the names of the nodes I have to control some later decisions in the script. The above can work if I'm in an interactive PBS job, but for the most part though I need to launch these jobs without intervention. When I try to do this by the stored filename:
>> fid = fopen(myjob) % returns invalid
fid =
-1
>> fgetl(fid)
??? Error using ==> fgetl at 44
Invalid file identifier. Use fopen to generate a valid file identifier.
Why, when I put in directly the value stored in myjob do I get a valid identifier, but when I put in myjob does it fail?
Thanks,
Andrew
Try this:
fid = fopen(deblank(myjob));
Looking at the way your output is formatted above, there appears to be an extra empty line appearing after the value of myjob is displayed, which indicates there may be a newline character appearing at the end of the string. This newline will cause the file name to not be recognized, so you can remove any trailing whitespace like this from a string with the function DEBLANK (or you can remove trailing and leading whitespace with the function STRTRIM).