Is there some way to export all variables from workspace on to an excel spreadsheet or a csv file, automatically. I have 100 + variables, and I do not want to iteratively export one variable at a time, referring each variable by name if possible.
You can write each variable to a different sheet in the spreadsheet:
S = whos; %// get all variables
xslfilename = 'savedFile.xslx';
for ii = 1:numel(S)
cmd = sprintf( 'xslwrite( xslfilename, %s, ''%s'' );', S(ii).name, S(ii).name );
eval( cmd ); %// not very happy with using `eval`...
end
Use whos to list all variables currently in workspace, and xlswrite to write each variable to a different sheet of the same xls file.
When reading the file you can use xlsinfo to get all sheets names
[status,sheets] = xlsfinfo( xslfilename ); %// get all sheet names = variable names
Now you can read each variable from each sheet using xslread.
This might not be what you're looking for, but if you're trying to save the workspace in order to load it back to MATLAB later, use the save and load functions.
Related
I'm trying to save list of variables from the workspace into a .mat file. The problem I encountered is that I'm trying to make a function of it, and that function should be able to handle a list of variables to be saved. I could loop as below:
vars = {'a','b','c'}; % names of variables
for k = 1:numel(vars)
save(filename,vars(k),'-append');
end
but this is not elegant for me and the flag -append slowed down the process.
I'm trying to achieve something like this:
vars = {'a','b','c'}; %names of variables
save(filename,vars);
Is this possible?
Since save expects each variable name as a separate input argument, you can use a comma-separated list generated from the cell array:
save(filename, vars{:})
I want to save data into a .mat file. When I add data for a second time it overwrites the previously stored content. I used the following command but is not working:
save('newAnswer.mat','dat','-append');
with
newAnswer.mat -> matFileName
dat -> arrayName
The '-append' option adds a new variable to the file. If you want to save the same variable, it will update the existing value of the variable in the file. If you think of it, makes sense—you can't have two variables with the same name in workspace, so you can't have two variables with the same name in the .mat file.
The workarounds are at least two: either 1) copy your variable in a new one, and save that variable:
dat1 = dat;
save('newAnswer.mat','dat1','-append');
or 2) create a cell array of values for each new value that you want to save, and update that variable:
%'before everything else'
dat_history = {};
%'. . .'
dat_history{end+1} = dat;
save('newAnswer.mat','dat_history','-append');
I'm writting a Matlab code that generates an array number and it should replace that each number in a text file (that already exists) and replace all instances with that. The number should be in string format. I've achieved this:
ita='"';
for i=1:size(z,2)
word_to_replace=input('Replace? ','s');
tik=input('Replacement? ','s');
coluna=input('Column? ');
files = dir('*.txt');
for i = 1:numel(files)
if ~files(i).isdir % make sure it is not a directory
contents = fileread(files(i).name);
fh = fopen(files(i).name,'w');
val=num2str(z(i,coluna));
word_replacement=strcat(tik,val,ita);
contents = regexprep(contents,'word_to_replace','word_replacement');
fprintf(fh,contents); % write "replaced" string to file
fclose(fh) % close out file
end
end
end
I want the code to open the file#1 ('file.txt'), find and replace all instances 'word_replacement' with 'word_to_replace' and save to the same file. The number of txt files is undefined, it could be 100 or 10000.
Many thanks in advance.
The problem with your code is the following statement:
contents = regexprep(contents,'word_to_replace','word_replacement');
You are using regular expressions to find any instances of word_to_replace in your text files and changing them to word_replacement. Looking at your code, it seems that these are both variables that contain strings. I'm assuming that you want the contents of the variables instead of the actual name of the variables.
As such, simply remove the quotations around the second and third parameters of regexprep and this should work.
In other words, do this:
contents = regexprep(contents, word_to_replace, word_replacement);
I'd like to save print all my variables in my workspace to a file together with their names in a certain way:
%<*firstVariableName>firstVariableValue(s)%</firstVariableName>
%<*secondVariableName>secondVariableValue(s)%</secondVariableName>
I'd like to save each variable on a new line. I've experimented with the function who, but I can't seem to get it to work.
I also have problem getting it to save as UTF-8, is there a simple encoding setting that could be changed?
The who function will save the names of the variables, but not the data in the variables. If you'd like to save the actual variables, you should use the save function, and save it to a *.mat file.
If you're trying to get a list of the names of the variables formatted in a certain way, I'd recommend doing this:
varlist = who(variables);
varlist is a cell array. You can then fopen to start writing to a file, iterate through the cell array using fwrite, and fclose it when you're done. By the way, when you iterate, you should use varlist{x}, which will return the string (whereas varlist(x) will return the cell).
Hope this helps. :)
This may not work in all cases, but it works well for numeric inputs. It may be modified appropriately as per your use case:
%Clears current workspace.
clear;
%Sample data.
a = 1;
b = [2 3];
new_var = [4;5];
%Relevant code.
my_var_list = who;
fid = fopen('my_var_list.txt','w');
for my_var_ii=1:numel(my_var_list)
my_temp_var = eval(my_var_list{my_var_ii});
my_temp_str = cellstr(strcat(sprintf('%%<*%s>', my_var_list{my_var_ii}), num2str(my_temp_var(:)'), sprintf('%%</%s>', my_var_list{my_var_ii})));
fprintf(fid, '%s\n', my_temp_str{:});
end
fclose(fid);
clear fid;
clear my_temp_var;
clear my_var_ii;
clear my_var_list;
clear my_temp_str;
Now my 'my_var_list.txt' will contain:
%<*a>1%</a>
%<*b>2 3%</b>
%<*new_var>4 5%</new_var>
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Save mat file from MATLAB
How to tell MATLAB to open and save specific files in the same directory
I have a set of CSV files and need to extract data to obtain plots. I use the following code to generate a variable file name in a loop and accordingly get the desired data.
P = dir('*.csv');
for m = 1:length(P)
P(m).data = csvread(P(m).name);
end
I now want to modify these CSV files (change the data values in the CSV files) before obtaining the desired data and then save these files to Excel format (.xls) within the loop.
Something like
for i = 1:length(P(m).data)
if P(m).data(i,1)< value1
P(m).data(i,2) = 0;
end
save P(m).xls P(m).data -ascii; % Gives error "save 'P(m).data' is not a valid variable name."
end
How do I save a file in Excel (.xls) format with a variable filename obtaining data from array in a loop?
Check out the MATLAB documentation for the save() function
You need to use the function-call syntax to use variable file names:
save(P(m).xls, P(m).data, '-ascii');
Edit: you seem to have new errors. I can see two things:
your P variable is a struct array, so it has more than 1 element -- save() can save only one file at a time;
2 xls is not a file of your struct, which has a name field.
To save your data, it will probably resemble this:
for m = 1 : length(P),
save(P(m).name, P(m).data, '-ascii');
end
If you want to replace the extension to avoid overwriting your files (I assume xls is what you wanted), this should do the trick:
for m = 1 : length(P),
name = P(m).name;
name = name(1:find(name,'.'));
name = [name '.xls'];
save(name, P(m).data, '-ascii');
end