how to import excel data as separate column vectors?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hey guys,
can someone please tell me how to import excel data as separate column vectors? I know how to do it trough the interface but i need to use it in a script :/
Thank you for your help
採用された回答
Star Strider
2020 年 2 月 29 日
The available functions inport Excel files as matrices (that are of course collections of column vectors) are readmatrix and xlsread. There are others such as readtable, however that imports them as table objects.
11 件のコメント
Viktor G.
2020 年 2 月 29 日
But there is an option to import the table as column vectors where each vector has the name of the variable from the table. I wouldn't be able to do that if i imported the whole table as just one matrix. I need to be able to pick any variable(column vector) by its name and not by its number of column.
Star Strider
2020 年 2 月 29 日
編集済み: Star Strider
2020 年 2 月 29 日
I have not found that to be an option with either function. They import matrices. You can then use ordinary array indexing to specify, and if necessary assign to separate variables, the columns you want.
EDIT —
I note than in a Comment to a different Answer, you wrote:
‘I have a table with a lot of variables as different columns like time, temperature etc. so what i am trying to do is for the be able to choose which variable they would like to see (which would be as a column vector) without me manualy creating each column as a vector. Is there any way to do this?’
Example —
T = array2table(randi(99,10,5), 'VariableNames',{'Time','Temperature','Humidity','BaroPres','DewPt'});
Choose = @(col) T(:,strcmp(T.Properties.VariableNames,col));
RH = Choose('Humidity')
Ok i get how this works but the problem I have with this way is that I can't use the plot option as i would like. I am trying to plot multiple variables from an excel file and I can't get around with it the legend always stays the same. My idea was that if they were all seprate vectors then i could solve the legend easier. This is the code i have.
clc
clear all
T = readtable('Book1.xlsx');
Time=T.DateTime;
ColumnYouNeed = input('Which variable would you like to plot: ','s');
mask = ismember(T.Properties.VariableNames,ColumnYouNeed);
NewTable = table2array(T(:,mask));
plot(Time,NewTable)
hold on
legend(ColumnYouNeed)
a=input('Would you like to plot another variable? ','s');
while a=='yes'
ColumnYouNeed = input('Which variable would you like to plot: ','s');
mask = ismember(T.Properties.VariableNames,ColumnYouNeed);
NewTable = table2array(T(:,mask));
plot(Time,NewTable)
legend(ColumnYouNeed)
a=input('Would you like to plot another variable? ','s');
end
Using my code (since I do not have your Excel file):
T = array2table(sortrows(randi(99,10,5),1), 'VariableNames',{'Time','Temperature','Humidity','BaroPres','DewPt'});
Choose = @(col) T(:,strcmp(T.Properties.VariableNames,col));
ColumnYouNeed = 'Humidity';
C = Choose(ColumnYouNeed)
figure
plot(T.Time, table2array(C))
grid
legend(ColumnYouNeed)
That worked when I tried it, including the legend.
Viktor G.
2020 年 2 月 29 日
But this is the easy way, as i said you wouldn't know how many of variables or which ones would the user want to see on the plot. The X axsis would always be time, but he may want to see the temperature druing the time, but may want to see the temperature, humidity and BaroPres during the time. This is where the problem comes from with the legend. I am really thankfull for all the help :)
check the problem with legend here please, i did this with the table you used
T = array2table(sortrows(randi(99,10,5),1), 'VariableNames',{'Time','Temperature','Humidity','BaroPres','DewPt'});
Choose = @(col) T(:,strcmp(T.Properties.VariableNames,col));
ColumnYouNeed = input('which column do you need? ' ,'s');
C = Choose(ColumnYouNeed);
plot(T.Time, table2array(C))
grid
legend(ColumnYouNeed)
hold on
answer=input('Would you like to also plot another column? ','s');
if answer=='yes'
ColumnYouNeed = input('which column do you need? ' ,'s');
C = Choose(ColumnYouNeed);
plot(T.Time, table2array(C))
grid
legend(ColumnYouNeed)
end
Create a cell array of ‘ColumnYouNeed’ and the legend entries work correctly:
T = array2table(sortrows(randi(99,10,5),1), 'VariableNames',{'Time','Temperature','Humidity','BaroPres','DewPt'});
Choose = @(col) T(:,strcmp(T.Properties.VariableNames,col));
ColumnYouNeed{1} = input('which column do you need? ' ,'s'); % Create AS Cell Array Elements
C = Choose(ColumnYouNeed{1});
plot(T.Time, table2array(C))
grid
legend(ColumnYouNeed{1})
hold on
answer=input('Would you like to also plot another column? ','s');
if strcmp(answer, 'yes')
ColumnYouNeed{2} = input('which column do you need? ' ,'s');
C = Choose(ColumnYouNeed{2});
plot(T.Time, table2array(C))
grid
legend(ColumnYouNeed)
end
It might be best to just put all that in a loop, incrementing ‘ColumnYouNeed’, rather than having the first part outside the loop and the rest inside.
You could then exit the loop with:
if strcmp(answer, 'yes')
ColumnYouNeed{2} = input('which column do you need? ' ,'s');
C = Choose(ColumnYouNeed{2});
plot(T.Time, table2array(C))
grid
legend(ColumnYouNeed)
else
return
end
I defer to you for that decision.
Sorry for totally spamming you, but this would only work if you know that he would plot at most two options. I made an error with putting if instead of while
T = array2table(sortrows(randi(99,10,5),1), 'VariableNames',{'Time','Temperature','Humidity','BaroPres','DewPt'});
Choose = @(col) T(:,strcmp(T.Properties.VariableNames,col));
ColumnYouNeed = input('which column do you need? ' ,'s');
C = Choose(ColumnYouNeed);
plot(T.Time, table2array(C))
grid
legend(ColumnYouNeed)
hold on
answer=input('Would you like to also plot another column? ','s');
while answer=='yes'
ColumnYouNeed = input('which column do you need? ' ,'s');
C = Choose(ColumnYouNeed);
plot(T.Time, table2array(C))
grid
legend(ColumnYouNeed)
end
Star Strider
2020 年 2 月 29 日
編集済み: Star Strider
2020 年 2 月 29 日
Try this:
T = array2table(sortrows(randi(99,10,5),1), 'VariableNames',{'Time','Temperature','Humidity','BaroPres','DewPt'});
Choose = @(col) T(:,strcmp(T.Properties.VariableNames,col));
k = 1;
answer = 'yes';
while strcmpi(answer,'yes')
answer=input('Would you like to plot a column? ','s');
if strcmpi(answer,'no')
break
end
ColumnYouNeed{k} = input('which column do you need? ' ,'s');
C = Choose(ColumnYouNeed{k});
Col(:,k) = table2array(C);
k = k + 1;
end
figure
plot(T.Time, Col)
grid
legend(ColumnYouNeed)
The logic worked when I ran it. Experiment to get different results.
EDIT — (29 Feb 2020 at 23:52)
This is much better and more efficient:
T = array2table(sortrows(randi(99,10,5),1), 'VariableNames',{'Time','Temperature','Humidity','BaroPres','DewPt'});
Prompt = {'Choose columns to plot: ','(Hold down CTRL to choose more than one)'};
List = T.Properties.VariableNames;
Colsc = listdlg('PromptString',Prompt, 'ListString',List(2:end), 'ListSize',[220 300])
Col = table2array(T(:,Colsc+1));
figure
plot(T.Time, Col)
grid
legend(List(Colsc+1))
Viktor G.
2020 年 3 月 1 日
Thank you so much :)
Star Strider
2020 年 3 月 1 日
As always, my pleasure!
その他の回答 (1 件)
Image Analyst
2020 年 3 月 1 日
Viktor, don't forget to look at my reply to you in this question of yours, which is pretty much the same.
1 件のコメント
Viktor G.
2020 年 3 月 1 日
Thank you so much, I was just trying to get an answer :)
カテゴリ
ヘルプ センター および File Exchange で 2-D and 3-D Plots についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
