Write pre-existing variable names and their values to excel

10 次查看(过去 30 天)
Hi Everyone,
I have a function that outputs a ton of variables, which all have useful names and numeric values.
I want to write the existing variable name to column 1 and the associated variable data to column 2, is this possible?
I've seen some xlswrite code examples, but none where the variable name and it's data are split before entry into the xls.
Cheers Nick

采纳的回答

Stephen23
Stephen23 2018-2-12
编辑:Stephen23 2018-2-12
load the data into a variable (as the load documentation states, it is a structure):
Results_Struct = load('1102 XRAIL_Z_Values.mat');
You can then get the variable names using fieldnames:
Results_Names = fieldnames(Results_Struct);
As the help and that error message tell you, the input to xlswrite must be a numeric, logical or cell array. You can easily get a cell array from the structure using struct2cell:
Results_Cell = struct2cell(Results_Struct);
Assuming that load returned a scalar structure then Results_Cell will have size px1x1, where p is the number of fields in the structure (i.e. the number of variables in the .mat file). You must ensure that the contents of each cell is a scalar: if this is not the case then you will need to deal with this in whatever way appropriate to your data. You could optionally convert this data to a numeric matrix.
You now have everything you need to call xlswrite.
Note that load-ing into a structure and using fieldnames is more efficient and less buggy than load-ing straight into the workspace and using who or whos.
  3 个评论
Oman Wisni
Oman Wisni 2018-10-14
I get error " unable to load file 1102 XRAIL_Z_Values.mat". where I can get this file ?
Vetei Vel
Vetei Vel 2020-7-2

I have many time series (by connecting to workspace block to model input and outputs) data saved in workspace after model run. Among these saved data I need few list of variables (146 - both bus and non bus variables) to be written to an Excel with its respective recorded values.. can any one help me on this please..

请先登录,再进行评论。

更多回答(3 个)

Pawel Jastrzebski
Pawel Jastrzebski 2018-2-12
If your variables are all numeric then consider the following code:
% create variables
a = 5;
b = 10;
c = 15;
d = 0;
d1 =1;
% get names
z_names = who;
% put names in the table
t = table(z_names);
% get values
z_values = [a b c d d1]';
% add values to the table
t.val = z_values;
% write table to excel
writetable(t,'excel.xlsx');% get values
z_values = [a b c d d1]';
t.val = z_values;
% write to excel
writetable(t,'excel.xlsx');

Nick Burnham
Nick Burnham 2018-2-12
I just tried your code and i get the following error:
"To assign to or create a variable in a table, the number of rows must match the height of the table."
However, i wasn't aware of the "who" function before. I think i can put your code with some of the xlswrite examples and get something to work, so, many thanks Pawel! :)
  1 个评论
Pawel Jastrzebski
Pawel Jastrzebski 2018-2-12
That message probably means that you're trying to add/create a new column in the table by using a row vector. If that's the case, transpose it first.

请先登录,再进行评论。


Nick Burnham
Nick Burnham 2018-2-12
编辑:Stephen23 2018-2-12
OK, I've got the variable names to appear correctly but not the values. Can you help?
Variable Names: WORKING CORRECTLY
load('1102 XRAIL_Z_Values.mat');
Results_Names= who; %
filename = 'testdata.xls';
sheet=1;
xlRange='A1';
xlswrite(filename,Results_Names,sheet,xlRange);
winopen('testdata.xls');
Non working variable values:
ERROR: Error using xlswrite (line 165)
Input data must be a numeric, cell, or logical array.
Results_Values=load('1102 XRAIL_Z_Values.mat');
sheet=1;
xlRange='A2';
xlswrite(filename,Results_Values,sheet,xlRange);

Community Treasure Hunt

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

Start Hunting!

Translated by