What on EARTH is wrong with the Matlab string representation? This is WEIRD.

조회 수: 2 (최근 30일)
So Matlab uses 'single quotes like these' to represent strings.
In most programming languages, whatever you put inside a string is sacrosanct. It doesn't affect anything outside the string, because, well, it's a string! It's DATA! The language shouldn't even be looking inside the string.
NOT SO IN MATLAB.
Consider the piece of code
thisBitmap2='resp04001.bmp';
path =[bitmapDir thisBitmap2];
movieFrame(1:h,w+1:2*w,:)=im2frame(imread(path));
This piece of code, of course, works fine.
I'd expect it to work fine whatever myString contains.
NOT SO, MATLAB! The code
thisBitmap2='resp04_001.bmp';
path =[bitmapDir thisBitmap2];
movieFrame(1:h,w+1:2*w,:)=im2frame(imread(path));
...produces the error
??? Conversion to double from struct is not possible.
The underscore makes Matlab think this string is a structure!
WHAT ON EARTH?
I'd just like to know why this is seen as a justifiable design decision.
All the best, Louise
  댓글 수: 2
Sean de Wolski
Sean de Wolski 2011년 3월 10일
what is bitmapDir ? Does it end/begin with a \ or / (OS dependent)?
Steve Eddins
Steve Eddins 2011년 3월 10일
Putting an underscore (or a period) in a string does not make MATLAB start treating it as a structure. Based on the information you've provided so far, I'm not sure why you jumped to that conclusion. You provided only the error message text, so we don't know what line the error actually occurred on, and we don't know what all the variables contained for both times you executed the code.

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

채택된 답변

Walter Roberson
Walter Roberson 2011년 3월 10일
At the time you attempt to run that code, movieFrame contains numeric data. im2frame(), however, returns a structure when it works. A "movie frame" is a structure with the fields "cdata" and "colormap" as described here. With movieFrame containing numeric data, Matlab attempts to convert the struct in to numbers (double) to store in the array, but it is unable to do so and emits the error.
Chances are that you do not want to convert the image to a movie frame there. For generality, however, you should check for indexed versus rgb images and convert any indexed image to rgb. The colormap for indexed images is returned as the second return value from imread
[data, map] = imread(path);
  댓글 수: 1
louise
louise 2011년 3월 10일
Dear me! What a phenomenally stupid question I asked.
Sorry for making unwarranted assumptions. They can get you into a load of trouble.

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

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2011년 3월 10일
First off, it wouldn't be the underscore but the . (period) since that's how struct fields are referenced.
I'm unable to replicate your problem:
myString= 'resp04_001.bmp';
imwrite(uint8(magic(5)),myString)
imread(myString)
ans =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
  댓글 수: 1
louise
louise 2011년 3월 10일
Dear me! What a phenomenally stupid question I asked.
Sorry for making unwarranted assumptions. They can get you into a load of trouble.

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by