Renaming an array in a script

33 ビュー (過去 30 日間)
Derek Ferguson
Derek Ferguson 2015 年 5 月 5 日
編集済み: Stephen23 2019 年 6 月 19 日
I've imported data from Excel into a structure, and then I want to save each column into a new array. After I input the data it is stored in a structure called 'Data' which is a 9458x247 double. I also have the header names stored in 'Params' which is a 1x247 cell.
I am trying to create a loop that looks like:
for i=1:length(Params);
par=strcat(Params(i));
p=Data(:,i);
rename(p, 'ParameterName', par);
end
Basically, I want the array to be named to the corresponding header name in Params, but Matlab keeps giving me errors saying 'Undefined function 'rename' for input arguments of type 'double'.
I can right-click on the array in the Workspace, and manually change the name, so I know it's possible, but I don't know how to set up a script to do this.
Anyone know how to do this?

回答 (3 件)

Michael Haderlein
Michael Haderlein 2015 年 5 月 5 日
編集済み: Michael Haderlein 2015 年 5 月 5 日
I see two options.
1) You know the names in advance (because they are all the same for every Excel file you open). Then, you can do something like
time=Data(:,1);
PosX=Data(:,2);
and so on.
2) You don't know the names in advance because they vary between the Excel files. Within seconds, this will go towards dynamic variable names, eval and so on. Don't do that. You already have the data in a cell and that's good. If you want, you can put it into a structure:
structData=cell2struct(Data,Params,2);
This can even be accessed dynamically, e.g.
structData.(Params{2})
will return the values of PosX assuming that Params{2} is 'PosX'.
  2 件のコメント
Michael Haderlein
Michael Haderlein 2015 年 5 月 6 日
Derek Ferguson's answer moved here:
"It won't let me do that because Data is 'double', and not 'cell'."
Michael Haderlein
Michael Haderlein 2015 年 5 月 6 日
Yes, you are right, seems like I was confused. Anyway, with
mat2cell(Data,size(Data,1),ones(1,size(Data,2)))
you can easily convert it to a cell first and then move on to a struct. At least I wouldn't know a direct way.

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


Derek Ferguson
Derek Ferguson 2015 年 5 月 5 日
It won't let me do that because Data is 'double', and not 'cell'.

Stephen23
Stephen23 2015 年 5 月 6 日
編集済み: Stephen23 2019 年 6 月 19 日

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by