I would like to write an else if statement from data imported from an excel file
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
If I had the following data from Excel:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/167390/image.png)
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"
0 Kommentare
Antworten (1)
Simon Henin
am 21 Sep. 2017
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 Kommentar
Siehe auch
Kategorien
Mehr zu Data Import from MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!