이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Matlab: Reading in data from an excel spreadsheet as a single integer
조회 수: 6 (최근 30일)
이전 댓글 표시
Louis
2011년 12월 22일
I am reading in data from an excel spreadsheet, but I am unable to use a comparison on what I read in. How can I read in a cell from an excel spreadsheet and compare what I get to a number? I'm pretty sure anything read from a spreadsheet is made into an array Here is the relevant code (edit: I'll give the whole code of what I have; edit2: Minor fixes, but being so new to MatLab, I think I'm not familiar with how to declare a variable):
>> for k = 2:4997
colF = 'F';
colE = 'E';
row = int2str(k);
entryF = strcat(colF, row);
entryE = strcat(colE,row);
millisecond = xlsread('someFile.xlsx', 1, entryE);
[~,message] = xlsread('someFile.xlsx', 1, entryF);
if millisecond == 1
soundMoment = 0;
elseif strcmp(message, 'probe_sound')
soundMoment = millisecond;
end
if soundMoment == 0
xlswrite('someFile.xlsx', 'preprobe', 1, entryF);
elseif millisecond > soundMoment
xlswrite('someFile.xlsx', 'postprobe', 1, entryF);
end
end
댓글 수: 25
Walter Roberson
2011년 12월 22일
Please check your file names: probably you should not have the * in the first one.
You should be using strcmp(message, 'probe_sound') instead of using == to compare the strings.
Aldin
2011년 12월 22일
You say entryE is number an entryF is text than you can in my opinion make this compare:
if entryE = 1
soundMoment = 0;
elseif strcmp(entryF,'probe_sound')
soundMoment = millisecond;
end
Aldin
2011년 12월 22일
The star and backslash is not needed. just type xlsread('someFile.xlsx',1,entryE)
Walter Roberson
2011년 12월 22일
EntryE and EntryF are locations in the file, not the entries themselves.
The "=" operator cannot be used in an "if" statement; "==" is the comparison operator.
Aldin
2011년 12월 22일
"good eye" :)
read data in excel and than xlswrtie the same data and than compare
what you want
Louis
2011년 12월 22일
The error I get in MatLAB is:
Error using == , Matrix dimensions must agree.
So I know the problem is with what I read in, the only issue is WHAT the datatype is of millisecond and message. Do I just need to convert message into a string, and millisecond into an integer?
Aldin
2011년 12월 22일
1 is an number you have to convert millisecond to number "str2num"
also for soundMoment
Louis
2011년 12월 22일
I don't follow. Does the ~ in [~,name] stand for something? And name is just what I want my variable to be named?
Louis
2011년 12월 22일
No error with the [~,message] bit, but now it says: Error using str2num (line 33), Requires string or character array input.
Walter Roberson
2011년 12월 22일
In 2009b onward, a ~ on the left-hand side of an assignment means to throw away the corresponding output argument. If you are using a version before 2009b, replace the ~ with any variable name that is not being used.
Aldin
2011년 12월 22일
delete all str2num functions in your code. And what is preprobe and postprobe it is the data to read in file???
Louis
2011년 12월 22일
Oops, preprobe and postprobe are supposed to be strings that I'm writing into that spot in the Excel spreadsheet. I thought in MatLAB that declarations weren't needed.
Louis
2011년 12월 22일
Did another small edit to the code, but I'm assuming to declare soundMoment, int soundMoment should work?
Walter Roberson
2011년 12월 22일
Declarations are not required. The problem is that your conditions do not specify what soundMoment should be set to if neither test is met.
Louis
2011년 12월 22일
I did "else soundMoment = -1;" and now there are no errors. The excel spreadsheet is quite big, so I'll have to wait to see if it came out as intended.
Louis
2011년 12월 23일
It works, except it's never listing anything as preprobe. I get all postprobe right, but preprobe isn't showing.
Aldin
2011년 12월 23일
In MATLAB there is no declaration such as in JAVA we have int, float, double, string...
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)