Matlab - Saving multiple variables from workspace in separate text files - matlab

How do I save multiple variables from workspace in separate text files with file name as the corresponding variable name?

You could use something like this:
vars = who;
for k = 1:numel(vars)
save(vars{k},vars{k});
end
The who function grabs all of the variables in your current workspace and, in practice, could be replaced by a cell array of strings of just the variable names that you would like to save.

Related

how do I load a list of variables from dot mat file

Suppose I have a big test.mat file, and I only wanted to load in several variables from it with variable names stored as varnames = {'var1','var10','var100'}. How would I transform varnames to be used in load?
Manually writing load('test.mat', 'var1','var10','var100') is not a good option since varnames could be long and dynamically generated. I also make it explicitly that individual variable names could have different lengths that char(varnames will fail.
load('test.mat', varnames) or load('test.mat', char(varnames)) or several other variants I tried do not work.
thanks.

Clearing a specific global variable in octave

I am trying to load the variables from a multiple .mat files using 'who' function and saving it in a variable 'A'. I am using a for loop for that. When I finish loading the first file and start loading the second file then 'A' shows variables in first .mat file as well. The problem is the function 'who' saves the variables as it is for multiple loops and I want to clear the 'who' after each loop. How can I do this. There is any way to clear a specific global variable.
for i=1:10; (10 mat files)
clear A;
clear who;
A=who; (all the variables in each mat file saved in A)
max(A(1,1); (finding max of variable A(1,1))
end
from the above code, if each .mat file has 5 variables then in the second loop the 'who' has 10 variables. the who is not cleared.
It's not entirely clear what you're trying to do because who (with no input arguments) returns a list of all variables in the current workspace not the variables within a file. For it to return a list of variables within a file you'd need to do something like:
vars = who('-file', filenames{i});
That being said, it looks like you actually want to load the variable A from all mat files that you've saved and find the maximum value of A across these files.
The better way to approach this is to specify an output to load which will load the data into a struct where each variable is stored as a separate field in the struct. You can also specify an additional input to load to specify that you'd only like to load variable A (in case there are other variables). You can then load each matfile into a separate struct and do your comparison
for k = 1:numel(filenames)
% Load variable A from this file into a struct
data(k) = load(filenames{k}, 'A');
end
% Now find the maximum value of A
maxA = max([data.A]);

How to save the matlab workplace variable including original file name?

I would like to know how I can save the matlab output files (i.e. matlab workplace variables), by including original file name.
e.g. I open a file (filename.mat) with load filename.mat. Then I run a code to do calculation and I get some workplace variables (e.g. flow, pressure). I want to save those variables as filename_flow.mat and filename_pressure.mat.
I will use the same code on different filename, so I would like to know how I can save my variables as mentioned above (i.e. including the original file's name)?
FileToBeLoaded = 'filename.mat';
[pathstr,filename,ext] = fileparts(FileToBeLoaded)
load([filename ext]);
%// calculate stuff
FlowVariable = %// some calculation
save([filename '_flow'],FlowVariable)
The same of course works for other names as well. You pull apart the original file name to its actual name and extension, and use the original name, add something (_flow in this case) and save that. The default of MATLAB is already to save to a .mat file, so that's taken care of automatically.

Get filename from Edit text in Matlab?

I want to write the file name in the gui edit text and save edit text as my filename. Than I could be able to save filename as I wish. For instance; My file is an Neural network file which could be save as *.mat file; here is my code
%network_name is my edit text
name = get(handles.network_name,'string');
name = net;
save name
But it doesn't work I can't manage file name from edit text :(
It saves as name that l wrote next to save (name.mat). Thanks for your any answer...
Why name = net;?
That aside, if you want to pass the filename as a variable to the save function, you need to use this syntax instead:
save(name)
save name is the "command form", and save('name') is the corresponding "function form". As you can see you can only give string inputs when using command form, whilst you actually want to pass a variable.
To conclude: if you are passing variables to a function, use function form.
If you want to save particular variables, use:
save(name,'net');
Note that name (which we want to be the string contained in the variable) isn't in quotes and net (the name of the variable we want to save) is.
A warning about this is actually buried in the documentation for save.
Do not use command form when any of the inputs, such as filename,
are variables.
save name
will save all of the variables in your workspace to a *.mat file called 'name'
Also your code is basically overwriting itself, line 2 sets the variable name to be a string, but then line 3 writes some data net to that variable.
I'm a little confused as to exactly what you want but I think you want to save the variable net to a mat file with the name that you read in the string from get(handles.network_name,'string')
If that is what you want to do then its just
save(get(handles.network_name,'string'), net)
If you want to save all the open variabile in the workspace then its just
save(get(handles.network_name,'string'))

load matlab data file

I would like to set in a file all constant,parameters and then I can access from another class to any variable
for example const file const.m
FILE='file';
EDIT='edit';
COLOR_RED ='red',
COLOR_BLUE ='Blue'
START ='Start'
.....
...
and from any other files if I would like to access to any variable :
a = const.EDIT
so that I haven't to set a ='file' in each file.
How could I do that with matlab ?
how could also use enumeration ?
You don't need a struct just use the save command: http://www.mathworks.com/help/techdoc/ref/save.html and specify which workspace variables you want to save. If there are small like in your example, they will be easy to read in notepad for access not in matlab. In matlab just use the load function.
For example create two variable in matlab:
Var1 = 'Red'
Var2 = [1 2; 3 4]
now type
save('myData.mat');
this should create a file called myData.mat in your current working folder.
Now in another Matlab script you want to set a variable to have the value you saved as Var1 so you can go
NewVar = load('myData.mat', Var1)
And you can easily edit the values in the .mat file in a text editor such as notepad or gedit (or emacs or ms word or whatever) if you add the -ascii parameter when saving.
But you can also rewrite a variable by using save() again and specifying the variable name.