How can i solve the issue of struct2table

조회 수: 28 (최근 30일)
Timmy Lehin
Timmy Lehin 2021년 5월 30일
댓글: Timmy Lehin 2021년 5월 31일
For a while now, i've been trying to implement the struct2table command in my code to like get my simulated variables in form f table but it has been futile despite watching,reading and trying all i have seen, still no postitive change. i need help, probably there is something i am not doing the right way until now.
intlinsol is a 768 x 1 (these are binary integers numbers of 1's and 0's) i got after simulating about 16 variables which has 48 binary enteries (of 1's and 0's) each
so then i wrote this:
tbl=struct2table(intlinsol);
vars= {'xwm1','xwm2','xdw1','xdw2','xvc1','xvc2','xmw','xtt','xsi','xest1','xest2','xov','xtm','xtd1','xtd2','xrf','uwm1','uwm2','udw1','udw2','uvc1','uvc2','umw','utt','usi','uest1','uest2','uov','utm','utd1','utd2','urf'};
outputvars= stack(tbl,vars,'NewDataVariableName','Amt','IndexVariableName','Var')
this is what matlab says:
"Error using struct2table (line 33) , Input structure must be a scalar structure, or a structure array with one column or one row."
Error in realmodifiedcode (line 213)
tbl=struct2table(intlinsol);

답변 (2개)

Matt J
Matt J 2021년 5월 30일
편집: Matt J 2021년 5월 30일
I don't know why you think struct2table is relevant to this problem if intlinsol is not a struct. You have also not described how the 16 variables are distributed over the 768 elements of linsol. If each successive group of 48 elements belong to one of the 16 variables, then you could build tbl as follows:
tbl=array2table( reshape(intlinsol,[],16) )
You then present 32 variable names vars in your code rather than the 16 that you say you have. Not sure how those are related to anything.
  댓글 수: 3
Timmy Lehin
Timmy Lehin 2021년 5월 30일
initally i use the code
showproblem(prob) and when i do that i can easily identify the other in which the problem was solved (e.g., maybe the variable 'xwm1' came first or another) but this worked when i had just 16 variables with 48 elements and i was able to see the full scope when i used that code (i.e., showproblem(prob)) but after restructuring the code to 32 variables with 48 elements, i used the same code but this time around, it wasn't showing me the full scope of how the problem was solved (e.g., the variable that came first and even i couldn't see the full objective function unlike when the first time i used 16 variables with 48 enteries)
that is why i resulted back to trying to use the code hat could automatically draft out the varaibles and the appropriate 48 elements
i used this:
tbl=array2table( reshape(intlinsol,[],32) );
vars={'Xwm1','Xwm2','Xdw1','Xdw2','Xvc1','Xvc2','Xmw','Xtt','Xsi','Xest1','Xest2','Xov','Xtm','Xtd1','Xtd2','Xrf','Uwm1','Uwm2','Udw1','Udw2','Uvc1','Uvc2','Umw','Utt','Usi','Uest1','Uest2','Uov','Utm','Utd1','Utd2','Urf'};
outputvars= stack(tbl,vars,'NewDataVariableName','Amt','IndexVariableName','Var')
NB: 'Xwm1' and co reprsents the variable names each with 48 elements already defined from the beginning
this is what i got:
Error using tabular/stack (line 115)
Unrecognized table variable name 'Xwm1'.
Error in COMPLETEDCODE1 (line 283)
outputvars=stack(tbl,vars,'NewDataVariableName','Amt','IndexVariableName','Var')
Timmy Lehin
Timmy Lehin 2021년 5월 30일
편집: Timmy Lehin 2021년 5월 30일
This is how i had defined my unknown 32 variableswith 48 elements each
nos=48;
% Define unknown main decision variables
Xwm1= optimvar('xwm1',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xwm2= optimvar('xwm2',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xdw1= optimvar('xdw1',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xdw2= optimvar('xdw2',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xvc1= optimvar('xvc1',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xvc2= optimvar('xvc2',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xmw = optimvar('xmw',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xtt = optimvar('xtt',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xsi = optimvar('xsi',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xest1=optimvar('xest1',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xest2=optimvar('xest2',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xov= optimvar('xov',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xtm = optimvar('xtm',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xtd1= optimvar('xtd1',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xtd2= optimvar('xtd2',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Xrf = optimvar('xrf',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
% Define unknown auxiliary decision variables
Uwm1= optimvar('uwm1',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Uwm2= optimvar('uwm2',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Udw1= optimvar('udw1',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Udw2= optimvar('udw2',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Uvc1= optimvar('uvc1',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Uvc2= optimvar('uvc2',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Umw = optimvar('umw',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Utt = optimvar('utt',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Usi = optimvar('usi',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Uest1=optimvar('uest1',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Uest2=optimvar('uest2',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Uov = optimvar('uov',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Utm = optimvar('utm',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Utd1= optimvar('utd1',nos,'LowerBound',0,'UpperBound',1,'Type','integer');
Utd2= optimvar('utd2',nos,'LowerBound',0,'UpperBound',1,'Type','integer');

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


Steven Lord
Steven Lord 2021년 5월 30일
You described intlinsol as "intlinsol is a 768 x 1 (these are binary integers numbers of 1's and 0's)" which to me implies that it is a logical or double vector. In that case, struct2table is not the right tool to convert that into a table array, since intlinsol is not a struct. Just call table on it to create a 768-by-1 table.
v = randi(5, 8, 1)
v = 8×1
1 1 5 4 3 2 5 2
t = table(v)
t = 8×1 table
v _ 1 1 5 4 3 2 5 2
  댓글 수: 8
Steven Lord
Steven Lord 2021년 5월 31일
Since that doesn't sound really related to the topic of this question you should ask it as a separate question with a descriptive title.
Timmy Lehin
Timmy Lehin 2021년 5월 31일
alright, thanks. i will do that

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by