How can I write both alphabets and numbers in .mat file on same cell?
Show older comments
I need to save the data in .mat file. But the data values has names such as a=0.3, b=9, alpha=-0.89. In the cell, both the name of the variable and the data value should be present. How can I do that? Also I tried to save the data in xls file but when I try to open, it says the file format and extension of the file don't match. The file could be corrupter or unsafe. How can I solve this problem also?
Accepted Answer
More Answers (1)
If I understand the situtation, you have a mat file with some variables and you want to write those variables' names and values in the form "a=0.3", etc., to an xls file. If that's accurate, here's something that will work for scalar numeric variables, as given in the question. If you have other sizes/types, you'll need to do something more sophisticated
S = load('vars.mat')
S_fields = fieldnames(S);
n_fields = numel(S_fields);
C = cell(n_fields,1);
for ii = 1:n_fields
C{ii} = sprintf('%s=%g',S_fields{ii},S.(S_fields{ii}));
end
C
writecell(C,'vars.xls')
4 Comments
ANANTA BIJOY BHADRA
on 23 May 2022
Edited: ANANTA BIJOY BHADRA
on 23 May 2022
OK. Just save the mat file like usual then:
% creating workspace variables
a = 0.3;
b = 9;
alpha = -0.89;
% save a mat file with all workspace variables:
save('vars.mat')
When the other person loads the mat file, they can see what the variables are:
S = load('vars.mat')
(cmdout is an artifact from running here on Answers - it would not appear on your machine.)
ANANTA BIJOY BHADRA
on 23 May 2022
Voss
on 23 May 2022
If you want the variable names and values to show up in the excel file like 'a=0.3', you can save the mat file first and then use the code in my answer.
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!