xlsread with changing starting point
Ältere Kommentare anzeigen
Hello all,
I've had some questions before, and everytime I did get an answer, so now I will try it again.
I'm using matlab to get some data out of a Excel document.
At the moment I'm using this code:
dir_struct = dir('*.xlsx');
for i = 1:numel(dir_struct)
number(i) = str2double(dir_struct(i).name(10:(10+numel(dir_struct(i).name)-16))); % get the number-string out of the filename and turn it into a number (I have fixed the format there)
end
[revised_numbers, order] = sort(number);
for i = 1:numel(order)
[num, tekst, raw] = xlsread(dir_struct(order(i)).name,'MISC','B9:C16');
I've received this code in a question earlier. I wanted to use xlsread, but the excel file changes in name over time, so the part where the excel file is mentioned, is now numerical.
This code works fine for me.
My next problem has this code involved.
In the worksheet MISC, the matrix defined is now B9:C16. B9 is i=1, B10 is i=2 etc.
Now, the start of i=1 changes. At the moment, B9 is i=1. But in the future, B12 or B13 for example can be i=1.
What I want is the following code, if possible ofcourse.
Above the data that I want, there is always the text: "Simulation input".
Is it possible for matlab, to 'search' the column B for this text, and set that cell to i=1? So that the place of the start can be everywhere in column B.
I hope that this is clear for you to understand. If not, I will change it and I will try to make it more sence.
I would like to thank you in advance!
Greets, Bart
Antworten (2)
Image Analyst
am 11 Sep. 2013
0 Stimmen
You can use ismember() to search your column for particular text string. It will give you the row(s) where it was found. Then set them to 1.
9 Kommentare
Image Analyst
am 18 Sep. 2013
[num, tekst, raw] = xlsread(dir_struct(order(i)).name,'MISC','B9:C16');
columnB = txt(:,2)
simulationRow = find(ismember(columnB, 'Simulation input'))
topRow = simulationRow+1;
It's a little trickier than that because the arrays don't all start at the same row in Excel. That is, if the first number is at row 3 and the first text is at row 8, for the num cell array num(1,1) will come from row 1 while txt(1,1) is not from row 3, it's from row 8.
Bart
am 25 Sep. 2013
Bart
am 25 Sep. 2013
Ilham Hardy
am 25 Sep. 2013
the txt should be tekst, i think..
So,
columnB = txt(:,2)
needs to be..
columnB = tekst(:,2)
Image Analyst
am 25 Sep. 2013
Let's see your code. Apparently you're not spelling txt, tekst, or text consistently. Let's just go with txt, which is how the MATLAB help calls it.
Bart
am 25 Sep. 2013
Image Analyst
am 25 Sep. 2013
Attaching your workbook to your message would make it easier if someone wants to try something. I can't get to it for several hours, if at all.
Bart
am 25 Sep. 2013
Bart
am 18 Sep. 2013
Kategorien
Mehr zu Data Import from MATLAB finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!