how to import excel data as separate column vectors?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
1 개 추천
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
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.
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))
Thank you so much :)
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
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
