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

3 vues (au cours des 30 derniers jours)
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 commentaires
Sean de Wolski
Sean de Wolski le 10 Mar 2011
what is bitmapDir ? Does it end/begin with a \ or / (OS dependent)?
Steve Eddins
Steve Eddins le 10 Mar 2011
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.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 10 Mar 2011
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 commentaire
louise
louise le 10 Mar 2011
Dear me! What a phenomenally stupid question I asked.
Sorry for making unwarranted assumptions. They can get you into a load of trouble.

Connectez-vous pour commenter.

Plus de réponses (1)

Sean de Wolski
Sean de Wolski le 10 Mar 2011
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 commentaire
louise
louise le 10 Mar 2011
Dear me! What a phenomenally stupid question I asked.
Sorry for making unwarranted assumptions. They can get you into a load of trouble.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by