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 个评论
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
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 个)
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!