So I have the function print_date which goes
fprintf('%d.%d.%d\n, date.day, date.month, date.year)
Which in this case prints out "20.5.1990"
But I want to remove the \n part because Im need to use this function in another. However, when I remove the \n, the output on the print_date becomes, I dont know how to explain. But the
the ">>" at the beginning of the command window becomes:
"20.5.1990>>" instead of "20.5.1990"
How do I fix this?
If you want to use the function print_date in another function, add a return value to print_date. The fprintf writes data to text file or to the console window. If the output string needs to be stored into a variable, use sprintf. You can then display the value of the variable using disp. Here's an example, but I recommend you to read the documentation for more info.
function s=print_date(date)
s=sprintf('%d.%d.%d', date.day, date.month, date.year);
disp(s);
end
Related
I would like to save the output of several evals ( don't know the input of those ) in a file. Now I don't know the tpye of those evals They could be arrays or strings or integer or anything, so I can not really write fprinft(fileID, '%s\n', eval(somethingsomething)); since I don't know what the second parameter should be. Is there a way a for me to save those in a file?
You can save the results of the eval in a local variable and check the resulting type with class(v) and use printf accordingly.
I believe you can just save result of eval with fwrite(), why would you use fprintf?
I have object of linkedList called rowdata(java.util.LinkedList) and I want to convert into cell Array of MATLAB. Any suggestion except follow?
for j=1:rowdata.size,
data{j} = rowdata.get(j-1)
end
As rowdata is having too much data, it will print every iterate of loop!
This is not an answer of how to do it but a very basic suggestion for Matlab use.
In Matlab, if you dont want an output to be printed in the command line, you should end the line with a semicolon.
Example:
a=3+4
a =
7
a=3+4;
"Nothing!"
So you can suppress all outputs by
for j=1:rowdata.size,
data{j} = rowdata.get(j-1);
end
This wont change the behaviour of your code, but it will avoid annoying command line prints.
For some reasons, I need to show a message each time save function is executed. All the code of my program is already written. That's why I want to override the saveMATLAB built-in function.
This is the function:
function save(varargin)
disp(['The file has been saved to ' varargin{1}])
builtin('save',varargin{:})
end
However, it doesn't work and MATLAB returns Error using save.
How can I solve this?
I am assuming varargin is cell array of strings, as in the built-in function save.
The problem is that your version of save doesn't "know" the variables of the caller function. You can use evalin function to evaluate save in the context of the caller function.
In order to do so, you should convert varargin to strings. One way to do so is
function save(varargin)
disp(['The file has been saved to ' varargin{1}])
cmd = ['builtin(''save'',' sprintf(repmat('''%s'',',1,nargin),varargin{:}) ];
cmd(end) = ')';
evalin('caller',cmd)
end
I have 31 models an I want to save each one in a specific file
this is my matlab function
formatspec='model%d'
for k = 1:length(libsvmFiles)
baseFileName = libsvmFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
[labels train]=libsvmread(fullFileName);
model=svmtrain(labels,train, '-t 2 -h 0');
file=sprintf(formatspec,k);
save file model;
but the problem is only the first file is saved and its name is 'file' tha's mean the value of the variable file is not evaluated
how can I solve this problem ?
As many Matlab functions, save can be used in function form (save(...)) or in command form (save ...). In the command form that you use, all the arguments are interpreted as strings. That means
save file model
is equivalent to
save('file', 'model')
For the second argument that is correct, because you want to refer to the variable with the name "model". For the first argument it is wrong, because you want to refer to the file name contained in the variable file. The correct syntax to use is therefore
save(file, 'model')
You're missing the parens for the save function. The model variable also needs to be listed as a string since you need to tell the save function the name of the variable, not the variable itself. See Matlab's documentation.
save(file, 'model');
Additionally you don't have an end to your for loop shown, which normally would just throw an error -- however later code might cause this loop to instead only run once. Otherwise you should check your libsvmFiles variable as it might be only length 1 or not be an array.
Say that, on a MATLAB interactive session, I call a function from a third party library output = long_execution(input). This function prints information via disp statements to the command window. I would like to capture the output of such disp statements on a text string that I can manipulate in MATLAB.
Is there a (hopefully easy) way of redirecting the output of disp to a text string? If so, how would you do it? (maybe via the overlading of disp?)
You can use evalc function to capture disp outputs. For example,
[T, output] = evalc('long_execution(input)');
Anything that would normally go to command window is captured into the output T.
If everything is going into stdout, you can use the diary function to capture that and write it to file, then after execution you can use any number of matlab file reading utilities to parse through it. You might also find the function tempdir and tempname useful in this context.