Mattias
Mattias

Reputation: 1131

MATLAB: Conversion to char from cell is not possible

So I've got a struct, settings, which contains three fields, averageValue, heightLabels and heights.

settings.averageValue = 7.5121 7.2742 7.4602 settings.heights = 105.1000 105.2000 105.3000

I'm looping through these with the following code:

for m = 1:length(settings.averageValue)
    settings.heightLabels(m) = {sprintf{'%.1f %s', settings.heights(m), 'm')};
end

However, I get the error "Conversion to char from cell is not possible.". Any ideas?

Upvotes: 0

Views: 5402

Answers (2)

Mattias
Mattias

Reputation: 1131

Okay guys I figured it out. It was a datatype question; my struct field was supposed to be a cell but it was a string. Rather embarrassing.

I resolved the problem with the following code when creating the struct fields:

settings.averageValue = {};
settings.heightLabels = {};

Thanks for your help!

Upvotes: 0

John Doe
John Doe

Reputation: 423

Well, I get your question now. Let's say you have variables A,B,C,D which you want to club together in a struct variable. Here's how you do it:

settings.A = A;
settings.B = B;
settings.C = C;
settings.D = D;

settings is a struct variable now, and if you want to access A, you refer to it as:

disp(settings.A) %display A
settings.A = 10; %edit A
newA = settings.A; %assign A to a new variable

Pass settings to a function, do whatever.

Upvotes: 1

Related Questions