필터 지우기
필터 지우기

I would like to write an else if statement from data imported from an excel file

조회 수: 7 (최근 30일)
farkab95
farkab95 2017년 9월 21일
편집: farkab95 2017년 9월 21일
If I had the following data from Excel:
How can I write an else if statement such as if Student Name is "Mary" then display Major name and class or else display "No Data"

답변 (1개)

Simon Henin
Simon Henin 2017년 9월 21일
You have a couple options. If you just want to find the row for Mary, you can use option 1. If you specifically want an if/else statement, you can loop through each row and output the info using option 2.
% read in the data
[~, exceldata] = xlsread('excel-file.xlsx');
% Option 1: find the relevant index for Mary
idx= find(strcmp(exceldata(:,1), 'Mary'));
fprintf('%s, Major: %s, Course: %s\n', exceldata{idx, 1:3});
% Option 2: using a loop
for i=1:size(exceldata,1),
if strcmp(exceldata{i,1}, 'Mary'),
fprintf('%s, Major: %s, Course: %s\n', exceldata{i, 1:3});
else
fprintf('no data\n');
end
end
  댓글 수: 1
farkab95
farkab95 2017년 9월 21일
편집: farkab95 2017년 9월 21일
Thank you that helped a lot! I needed something like option 2. Is there a way so that only one result is displayed instead of an output for each row? For example, if the conditions are met, only Mary's name and major is displayed and if they are not, "No Data" is displayed only once.
Thank you again!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by