transform table variable into struct variable.

조회 수: 6 (최근 30일)
jana nassereddine
jana nassereddine 2024년 12월 26일
댓글: Walter Roberson 2024년 12월 27일
Hello,
I have a table that is used as a disturbance in a model predictive controller, and I need to input this table as a measured disturbance. However, the measured disturbance, like any variables in the model predictive controller, is formatted as a struct. I attempted to convert the struct to a cell array using the script, but I received an error stating that the struct cannot be changed. Therefore, I need to convert the variable table into a struct. Do you have any suggestions on how to do this?

채택된 답변

Walter Roberson
Walter Roberson 2024년 12월 26일
Perhaps use table2struct ?
Note that your posted code creates a cell array, U and uses () indexing to try to set U(1) and so on. In order for that to work, the right hand side of the assignment needs to be a cell array scalar, or something convertable to a cell array scalar. nlobj.MV(1) does not appear to be convertable to a cell. Your code would make more sense if you did, for example,
U{1} = nlobj.MV(1);
On the other hand, I note that all of the entries appear to be numeric scalars, so I wonder whether you should have instead initialized
U = zeros(1,5);
  댓글 수: 1
Walter Roberson
Walter Roberson 2024년 12월 27일
nlobj_struct = cell2struct(table2cell(nlobj), nlobj.Properties.VariableNames);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Downloads에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by