Clearing a specific global variable in octave - matlab

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]);

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.

Matlab - Saving multiple variables from workspace in separate text files

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.

Save workspace of unknown workspace in Matlab

Is it possible to save workspace variables from a function that I am calling and cannot explicitly edit without file I/O?
I know I can use the save function to save all of the variable names in a workspace, but what if I wanted to save the workspace variables from a function that I am calling, like a built in function (mean, sum, etc).
I would like to save all of the variables from a function's workspace before it returns back to the function I am writing, and I would like to do it without opening the file each time and adding an extra line of code; is this possible?
In case anyone is interested:
I have yet to find a solution to the exact question I asked, but I found a solution that works well enough with a little extra file tracking.
Using the function onCleanup, you can specify that all the variables be saved right before the function returns to the caller. Using this and a little file parsing, you can open the code in question as a simple text file, and insert the onCleanup code anywhere in the file (easier than inserting save as the last line). Then, you can run the code and track the new .mat file using the previous file name or any naming method you choose.
That will enable you to save all the variables in a workspace just before the function exits, but it does require file parsing, see simple example below:
readFile = fopen('filename.m');
writeFile = fopen(['filename_new.m']);
%Ignore the first line (hopefully the function header, may need extra parsing if not)
functionHeader = fgets(readFile);
%Print the function header
fprintf(writeFile,functionHeader);
%Print the clean-up code
%NOTE: This can go anywhere in the file
fprintf(writeFile,sprintf('onCleanup(#()save(''%s.mat''))\n',filename)));
nextLine = fgets(readFile);
while ischar(nextLine)
fprintf(writeFile,nextLine);
nextLine = fgets(readFile);
end
With the above, a new file is created (filename_new.m) which needs to be run, and will create a mat file (filename.mat) with all of the workspace variables in it.
eval(newFileName(1:end-2));
Now, by tracking the .mat file, you can do whatever is necessary after this point. For my purposes, I was interested in the memory used by the said function, which is available by accessing the mat object of the .mat file.
matObj = matfile('filename.mat');
stats = whos(matObj);
fileSize = sum([stats.bytes]);
Try the "save" function.
Add this line in your called function:
save('filename')
Here is my sample code:
a=10; b=6;
c=addition(a,b);
And the function is defined as:
function [out]=addition(a,b)
out=a+b;
temp1=a;
temp2=b;
temp3=a-b;
save('C:\data.mat');
end

Load multiple .mat files for processing

In MatLab I have (after extensive code running) multiple .mat files outputted to .mat files. The actual matlab name of each .mat file is called results but I've used the save command to write them to different files. A small subset of the files looks like this:
results_test1_1.mat
results_test1_2.mat
results_test1_3.mat
results_test1_4.mat
results_test2_1.mat
results_test2_2.mat
results_test2_3.mat
results_test2_4.mat
Now I want to compare the results for each test, which means I have to load in all four .mat files and combine them in a graph. Reading in one file and making the eventual graph is no problem. But since all files have the same matlab name results, iteratively loading them is not an option (at least, not one that I know of yet) since in the end only file 4 remains since it rewrites the previous ones.
Is there a way to load all these files and store them in different variables in a structure (regarding only one test set)? Because doing all this manually is a hell of a lot of work.
I've tried to use this method: Load Multiple .mat Files to Matlab workspace but I get an Invalid field name error on loaded.(char(file)) = load(file);
You can load into a variable (preferably a cell array)
results = cell( 2, 4 ); % allocate
for testi=1:2
for resi = 1:4
filename = sprintf('results_test%d_%d.mat', testi, resi );
results{testi,resi} = load( filename );
end
end
Now you have all the results stored in results cell array and you may access the stored variables, e.g.,
results{1,3}.someVar % access variable someVar (assuming such variable was saves to the corresponding mat file

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.