Error using vertcat Dimensions of matrices being concatenated are not consistent.

Good day. I am making a gui about the brayton cycle with input parameters. I am trying to display the previous parameters in a uitable but the error keeps popping out. This is the piece of code in my callback function.
Prev_val = [r_p,T_3c,n_comp * 100,n_turb * 100,n_th,W_net];
Prev_val = [Prev_val; get(handles.Prev_calc, 'data')];
set(handles.Prev_calc,'data',Prev_val);
When I run the program by commenting out line 2 first then bringing it back the error is not appearing.

 Réponse acceptée

Matt J
Matt J le 29 Déc 2016
Modifié(e) : Matt J le 29 Déc 2016
One way to trap the error is to insert code as follows
Prev_val = [r_p,T_3c,n_comp * 100,n_turb * 100,n_th,W_net];
columns1 = size(Prev_val , 2);
columns2 = size( get(handles.Prev_calc, 'data') , 2 );
if columns1~=columns2
disp 'Unexpected size mismatch'
columns1, columns2,
keyboard
end
Prev_val = [Prev_val; get(handles.Prev_calc, 'data')];
When you re-run, the code will stop at the point just before the error occurs, allowing you to inspect the sizes of the matrices you are trying to concatenate and analyze what led to them being that way. You can accomplish much the same thing by Setting Breakpoints.

6 commentaires

I have tried inserting the code and the error still pops out and the table is still empty. I have tried the same idea by using
if size(Prev_val, 2)==size(get(handles.Prev_calc, 'data'), 2)
but still no luck
Matt J
Matt J le 29 Déc 2016
Modifié(e) : Matt J le 29 Déc 2016
The inserted code is not meant to prevent the error. It is meant to help you find out why it is occurring. Again, it will stop the code at the point where the error is occurring to let you inspect the state of the variables and analyze why they don't have the right size.
There is no path to a solution for this other than for you to determine why Prev_val and get(handles.Prev_calc, 'data') don't have the same number of columns.
Is there any way to preset the dimensions of get(handles.Prev_calc, 'data')?
Matt J
Matt J le 29 Déc 2016
Modifié(e) : Matt J le 29 Déc 2016
The dimensions of get(handles.Prev_calc, 'data') will be the dimensions of whatever variable you have placed in handles.Prev_calc.data.
So I tried this,
data = get(handles.Calculations, 'Data');
if size(data,2)==size(Out,2)
data
Out
data = [data; Out];
else
end
set(handles.Calculations, 'Data', data);
and it displayed like this:
data =
'' '' '' '' '' ''
'' '' '' '' '' ''
'' '' '' '' '' ''
'' '' '' '' '' ''
Out =
8.0000 900.0000 100.0000 100.0000 44.7955 288.9921
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in ME443_beta>Calculate_Callback (line 92)
data = [data; Out];
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in ME443_beta (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)ME443_beta('Calculate_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
What condition did I miss?
Well, it's very strange that you would get that particular error messagefrom vertcat.
But basically, the problem is that you are trying to join a numeric variable "Out" to the cell array of strings, "data". They both need to be cell arrays.
One possibility is to convert 'Out" to a cell array of strings as well,
OutCell=textscan(num2str(Out),'%s');
data = [data; OutCell{1}.'];
A different possibility is to split Out into cells, but keep the cell contents in numeric form,
data = [data; num2cell(Out)];
Which one you need depends what you are going to do with the concatenated data later.

Connectez-vous pour commenter.

Plus de réponses (1)

[ones(2,1) ; ones(1,3)]
The above will throw a error, as dimensions are not compatible to join/concatenate. Where as the below
[ones(2,1) ; ones(3,1)]
works, as the dimensions of matrices are compatible to join. Check the dimensions of your matrices which you are joining.

1 commentaire

I am really new to Matlab and I don't really know what to do. I have screened the actual gui and the callback code. Your guidance would be appreciated.
% --- Executes on button press in Calc1.
function Calc1_Callback(hObject, eventdata, handles)
% hObject handle to Calc1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Air Standard Brayton Cycle Program
%Setting initial parameters
T_1 = 293;
k = 1.4;
C_p = 1.0045;
%Input parameters
r_p = str2double(get(handles.Comp_ratio1,'String'));
n_turb = (str2double(get(handles.Turb_eff1,'String')))/100;
n_comp = (str2double(get(handles.Comp_eff1,'String')))/100;
T_3c = str2double(get(handles.Max_Temp1,'String'));
%Calculation of missing variables
T_3 = T_3c + 273;
T_2s = T_1*(r_p^((k - 1)/k));
T_4 = T_3*(r_p^((1 - k)/k));
W_turbs = 1.0045*(T_3 - T_4);
W_comps = 1.0045*(T_2s - T_1);
W_comp = W_comps/n_comp;
W_turb = W_turbs*n_turb;
W_net = W_turb - W_comp; %Calculate the actual net work
T_2 = (W_comp + 1.0045 * T_1)/C_p;
Q_in = 1.0045*(T_3 - T_2);
n_th = (W_net/Q_in) * 100; %Calculate the actual thermal efficiency
set(handles.Therm_eff1, 'String', n_th); %Display the output variables
set(handles.Net_Work1, 'String', W_net);
%%set(handles.Prev_calc,'data',[r_p,T_3c,n_comp * 100,n_turb * 100,n_th,W_net]);
%%Prev_val = get(handles.Prev_calc, 'data');
Prev_val = [r_p,T_3c,n_comp * 100,n_turb * 100,n_th,W_net];
%Prev_val = [Prev_val; get(handles.Prev_calc, 'data')]; - if i comment this out then bring this back again it works
set(handles.Prev_calc,'data',Prev_val);

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by