I think it would be useful, at least I find so, and I searched for but did not find anythign appropriate.
Supposing you have a set of lines that use stdout, or these lines are included in a function. Then I need to change the output stream to a file. But not with diary() and such.
Example:
ShowResults(...) % this is a function containing a lot of fprintf('asdasdasd', ...)
% which by default shows messages on monitor
then I need something like:
ShowResults(...) % this will now output to monitor
setOutputHandler(my_file_pointer); % setup redirection
ShowResults(...) % this will now output to the file
setOutputHandler(stdout);
or even better something like:
setOutputHandler(stdout, my_file_pointer);
ShowResults(...) % this will now output to the file and monitor at the same time
setOutputHandler(stdout);
Especially if you use fprintf in your function, the easiest would be to define an additional input that you'd use as first argument in each call to fprintf
By default, that additional input would be set to 0, which means fprintf prints to screen. Alternatively, you can pass a file identifier created by fopen to showResults, so that fprintf would write to file.
If that is infeasible, you can always use capturedOutput = evalc('showResults(...)'), which will capture all the outputs in an array, from which you'd write to file.
Related
Is it possible to use checkcode or matlab.internal.codeanalyzer to parse a string of code, for example:
for i=1:100 a*b(i); end
without first putting that in a file and then calling checkcode or the internal parser.parse on that file. I want to check thousands of strings of code but dynamically without putting them in a file before each check. Ideally, I would like to be able to parse a string with the code in and have that parsed.
To be more clear incase there are other functions that could be useful, I actually want to parse a string and count the number of syntax errors, and find their location etc.
Seems mlintmex has a -text option which one can use to pass a string. It needs a string and a filename, however, it seems not to even use the filename, so not sure why. Furthermore, I can't figure out why, but when using -text there needs to be at least one parseable command. Therefore instead of calling
fid = fopen('tmp.m','wt');
fprintf(fid, '-mean[1:10)');
fclose(fid);
info = checkcode('tmp.m', '-fullpath');
one can use the following which has a great increase in performance:
% Get the errors with text mode
info = checkcode(['1;','-mean[1:10)'], 'tmp.m', '-fullpath', '-text');
% Reduce the columns to take in to account the '1;' in the checkcode call above.
for i=1:numel(info)
info(i).column = info(i).column - 2;
end
Using tic and toc in a loop for 1000 times, I get ~9 seconds for the file version and ~3-4 seconds for the -text version.
I am creating a matlab application that is analyzing data on a daily basis.
The data is read in from an csv file using xlsread()
[num, weather, raw]=xlsread('weather.xlsx');
% weather.xlsx is a spreadsheet that holds a list of other files (csv) i
% want to process
for i = 1:length(weather)
fn = [char(weather(i)) '.csv'];
% now read in the weather file, get data from the local weather files
fnOpen = xlsread(fn);
% now process the file to save out the .mat file with the location name
% for example, one file is dallasTX, so I would like that file to be
% saved as dallasTx.mat
% the next is denverCO, and so denverCO.mat, and so on.
% but if I try...
fnSave=[char(weather(i)) '.mat'] ;
save(fnSave, fnOpen) % this doesn't work
% I will be doing quite a bit of processing of the data in another
% application that will open each individual .mat file
end
++++++++++++++
Sorry about not providing the full information.
The error I get when I do the above is:
Error using save
Argument must contain a string.
And Xiangru and Wolfie, the save(fnSave, 'fnOpen') works as you suggested it would. Now I have a dallasTX.mat file, and the variable name inside is fnOpen. I can work with this now.
Thanks for the quick response.
It would be helpful if you provide the error message when it doesn't work.
For this case, I think the problem is the syntax for save. You will need to do:
save(fnSave, 'fnOpen'); % note the quotes
Also, you may use weather{i} instead of char(weather(i)).
From the documentation, when using the command
save(filename, variables)
variables should be as described:
Names of variables to save, specified as one or more character vectors or strings. When using the command form of save, you do not need to enclose the input in single or double quotes. variables can be in one of the following forms.
This means you should use
save(fnSave, 'fnOpen');
Since you want to also use a file name stored in a variable, command syntax isn't ideal as you'd have to use eval. In this case the alternative option would be
eval(['save ', fnSave, ' fnOpen']);
If you had a fixed file name (for future reference), this would be simpler
save C:/User/Docs/MyFile.mat fnOpen
I need to suspend the program until the variable is created. I point to the data file, then open uiimport to specify the range of data. And at this moment I have to suspend the program until the variable is created.
%find file
[FileName,PathName] = uigetfile({'*.xls;*.xlsx', 'Excel files(*.xls, *.xlsx)'},'Укажите Excel-файл с данными');
%open import wizard
uiimport(strcat(PathName, FileName));
% here i need to suspend the program until the variable is created
You must specify an output variable when calling uiimport. If you do this, no lines after your call to uiimport will be executed until uiimport has completed (the user has chosen data to import or not).
data = uiimport(fullfile(PathName, FileName));
% Do stuff with data
disp(data.value)
If for some reason, you did need to wait until a variable existed, you could use a while loop combined with exist, but in general this is a marker of poor program design.
while ~exist('variablename', 'var')
% Do something that may define the variable
end
Update
If you're simply reading an excel file, it is likely easier to use xlsread to do so:
data = xlsread(filename, -1);
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.