JavaEar 专注于收集分享传播有价值的技术资料

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')};

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


  1. 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.

  2. 参考答案2
  3. 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!