フィルターのクリア

How can I write both alphabets and numbers in .mat file on same cell?

2 ビュー (過去 30 日間)
ANANTA BIJOY BHADRA
ANANTA BIJOY BHADRA 2022 年 5 月 23 日
回答済み: Image Analyst 2022 年 5 月 23 日
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?

採用された回答

Image Analyst
Image Analyst 2022 年 5 月 23 日
You can list the variables you want to save as arguments in the save function
a=0.3
b=9;
alpha=-0.89
unUsedVariable = 345;
x = 40;
% Save a, b, and alpha ONLY in variables.mat
save('variables.mat', 'a', 'b', 'alpha');
% Save a, b, and alpha ONLY in variables.xlsx. Save to a cell array first.
ca = {'a', a;
'b', b;
'alpha', alpha};
writecell(ca, 'variables.xlsx');
% Open in in Excel (if using Windows)
winopen('variables.xlsx')

その他の回答 (1 件)

Voss
Voss 2022 年 5 月 23 日
編集済み: Voss 2022 年 5 月 23 日
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 = struct with fields:
a: 0.3000 alpha: -0.8900 b: 9
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
C = 3×1 cell array
{'a=0.3' } {'alpha=-0.89'} {'b=9' }
writecell(C,'vars.xls')
  4 件のコメント
ANANTA BIJOY BHADRA
ANANTA BIJOY BHADRA 2022 年 5 月 23 日
Thank you for the answer. Also, what should be the format for excel file?
Voss
Voss 2022 年 5 月 23 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by