GUI Interpolation calculator, Indices exceeds array?

2 ビュー (過去 30 日間)
Tyde Hilderbrandt
Tyde Hilderbrandt 2021 年 12 月 3 日
編集済み: Tyde Hilderbrandt 2021 年 12 月 4 日
I have been working on a GUI interpolation calulator I am required to code. I have gotten the majority of it completed, then I changed one thing and the code broke. I can not figure out why on line 136, handle.P_r "Index exceeds the number of array elements (1)." when its 1x25. I am only working with the edit_h_call back which I have marked with %*
function varargout = Interp_calc(varargin)
% INTERP_CALC MATLAB code for Interp_calc.fig
% INTERP_CALC, by itself, creates a new INTERP_CALC or raises the existing
% singleton*.
%
% H = INTERP_CALC returns the handle to a new INTERP_CALC or the handle to
% the existing singleton*.
%
% INTERP_CALC('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in INTERP_CALC.M with the given input arguments.
%
% INTERP_CALC('Property','Value',...) creates a new INTERP_CALC or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Interp_calc_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Interp_calc_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Interp_calc
% Last Modified by GUIDE v2.5 03-Dec-2021 16:27:07
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Interp_calc_OpeningFcn, ...
'gui_OutputFcn', @Interp_calc_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Interp_calc is made visible.
function Interp_calc_OpeningFcn(hObject, eventdata, handles, varargin)
clc
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Interp_calc (see VARARGIN)
% Choose default command line output for Interp_calc
handles.output = hObject;
%Get the Value of the radio button for all funtions
eng=get(handles.edit_radiobutton,'value');
handles.eng=eng;
%Metric Units
T_m=[200 210 220 230 240 250 260 270 280 285 290 295 298 300 305 310 315 320 325 330 340 350 360 370 380];
h_m=[199.97 209.97 219.97 230.02 240.02 250.05 260.09 270.11 280.13 285.13 290.16 295.17 298.18 300.19 305.22 310.24 315.27 320.29 325.31 330.34 340.42 350.49 360.58 370.67 380.77];
P_r_m=[.3363 .3987 .4690 5477 .6355 .7329 .8405 .9590 1.0889 1.1584 1.2311 1.3068 1.3543 1.3860 1.4686 1.5546 1.6442 1.7375 1.8345 1.9352 2.149 2.379 2.626 2.892 3.176];
v_r_m=[1707.0 1512.0 1346.0 1205.0 1084.0 979.0 887.8 808.0 738.0 706.1 676.1 647.9 631.9 621.2 596.0 572.3 549.8 528.6 508.4 489.4 454.1 422.2 393.4 367.2 343.2];
handles.T_m=T_m;
handles.h_m=h_m;
handles.P_r_m=P_r_m;
handles.v_r_m=v_r_m;
%Imperial Untis
T_e=[360 380 400 420 440 460 480 500 520 537 540 560 580 600 620 640 660 680 700 720 740 760 780 800 820];
h_e=[85.97 90.75 95.53 100.32 105.11 109.90 114.69 119.48 124.27 128.10 129.06 133.86 138.66 143.47 148.28 153.09 157.92 162.73 167.73 172.39 177.23 182.08 186.94 191.81 196.69];
P_r_e=[.3363 .4061 .4858 .5760 .6776 .7913 .9182 1.0590 1.2147 1.3593 1.3860 1.5742 1.7800 2.005 2.249 2.514 2.801 3.111 3.446 2.806 4.193 4.607 5.051 5.526 6.033];
v_r_e=[496.6 346.6 305.0 270.1 240.6 215.33 193.65 174.90 158.58 146.34 144.32 131.78 120.70 110.88 102.12 94.30 87.27 80.96 75.25 70.07 65.38 61.10 57.20 53.63 50.35];
handles.T_e=T_e;
handles.h_e=h_e;
handles.P_r_e=P_r_e;
handles.v_r_e=v_r_e;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Interp_calc wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Interp_calc_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
%*****************************************************************************
%*****************************************************************************
function edit_h_Callback(hObject, eventdata, handles)
% hObject handle to edit_h (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_h as text
% str2double(get(hObject,'String')) returns contents of edit_h as a double
% --- Executes during object creation, after setting all properties.
n=0;
h_v=get(handles.edit_h, 'string');
h_v=str2double(h_v);
f=ismember(h_v,handles.h_m);%find if intput value is in chart
for i=1:length(handles.h_m)
hvalm=handles.h_m(i);
for j=1:length(handles.h_e)
hvale=handles.h_m(j);
if f==0 && handles.eng==0 && h_v>hvalm
i=n+1;
n=n+1;
hminidx=i;
hminval=handles.h_m(n);
hmaxidx=i+1;
hmaxval=handles.h_m(n+1);
handles.T=handles.T_m(hminidx)+((h_v-hminval)/(hmaxval-hminval))*(handles.T_m(hmaxidx)-handles.T_m(hminidx));
handles.P_r=handles.P_r_m(hminidx)+((h_v-hminval)/(hmaxval-hminval))*(handles.P_r_m(hmaxidx)-handles.P_r_m(hminidx));
handles.v_r=handles.v_r_m(hminidx)+((h_v-hminval)/(hmaxval-hminval))*(handles.v_r_m(hmaxidx)-handles.v_r_m(hminidx));
set(handles.edit_T,'string',handles.T);
set(handles.edit_P_r,'string',handles.P_r);
set(handles.edit_v_r,'string',handles.v_r);
elseif f==1 && handles.eng==0 && h_v==hvalm
handles.T=handles.T_m(i);
handles.P_r=handles.P_r_m(i);
handles.v_r=handles.v_r_m(i);
set(handles.edit_T,'string',handles.T);
set(handles.edit_P_r,'string',handles.P_r);
set(handles.edit_v_r,'string',handles.v_r);
elseif f==0 && handles.eng==1 && h_v>hvale
j=n+1;
n=n+1;
hminidx=j;
hminval=handles.h_e(n);
hmaxidx=j+1;
hmaxval=handles.h_e(n+1);
handles.T=handles.T_e(hminidx)+((h_v-hminval)/(hmaxval-hminval))*(handles.T_e(hmaxidx)-handles.T_e(hminidx));
handles.P_r=handles.P_r_e(hminidx)+((h_v-hminval)/(hmaxval-hminval))*(handles.P_r_e(hmaxidx)-handles.P_r_e(hminidx));
handles.v_r=handles.v_r_e(hminidx)+((h_v-hminval)/(hmaxval-hminval))*(handles.v_r_e(hmaxidx)-handles.v_r_e(hminidx));
set(handles.edit_T,'string',handles.T);
set(handles.edit_P_r,'string',handles.P_r);
set(handles.edit_v_r,'string',handles.v_r);
end
end
end
% if f==0
% handles.T=handles.T_m(hminidx)+((h_v-hminval)/(hmaxval-hminval))*(handles.T_m(hmaxidx)-handles.T_m(hminidx));
% handles.P_r=handles.P_r_m(hminidx)+((h_v-hminval)/(hmaxval-hminval))*(handles.P_r_m(hmaxidx)-handles.P_r_m(hminidx));
% handles.v_r=handles.v_r_m(hminidx)+((h_v-hminval)/(hmaxval-hminval))*(handles.v_r_m(hmaxidx)-handles.v_r_m(hminidx));
%
% set(handles.edit_T,'string',handles.T);
% set(handles.edit_P_r,'string',handles.P_r);
% set(handles.edit_v_r,'string',handles.v_r);
% else
%
% set(handles.edit_T,'string',T_m);
% set(handles.edit_P_r_m,'string',P_r_m);
% set(handles.edit_v_r_m,'string',v_r_m);
% end
guidata(hObject,handles)
function edit_h_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_h (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_P_r_Callback(hObject, eventdata, handles)
% hObject handle to edit_P_r (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_P_r as text
% str2double(get(hObject,'String')) returns contents of edit_P_r as a double
% --- Executes during object creation, after setting all properties.
n=0;
P_r_v=get(hangles.edit_P_r, 'Stirng')
P_r_v=str2doubel(P_r_v)
f=ismember(P_r_v,handles.P_r_m);
for i=1:length(P_r_m)
P_rvalm=handlesP_r_m(i);
if f==0 && edit_P_r>P_rvalm
i=n+1;
n=n+1;
P_rminidx=i;
P_rminval=handles.P_r_m(n);
P_rmaxidx=i+1;
P_rmaxval=handles.P_r_m(n+1);
end
end
handles.T=T_m(P_rminidx)+((edit_P_r_Callback-P_rminval)/(P_rmaxval-P_rminval))*(T_m(P_rmaxidx)-T_m(P_rminidx));
handles.h=h_m(P_rminidx)+((edit_P_r_Callback-P_rminval)/(P_rmaxval-P_rminval))*(h_m(P_rmaxidx)-h_m(P_rminidx));
handles.v_r=v_r_m(P_rminidx)+((edit_P_r_Callback-P_rminval)/(P_rmaxval-P_rminval))*(V_r_m(P_rmaxidx)-V_r_m(P_rminidx));
set(handles.edit_T,'string',handles.T);
set(handles.edit_h,'string',handles.h);
set(handles.edit_v_r,'string',handles.v_r);
guidata(hObject,handles)
function edit_P_r_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_P_r (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_v_r_Callback(hObject, eventdata, handles)
% hObject handle to edit_v_r (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_v_r as text
% str2double(get(hObject,'String')) returns contents of edit_v_r as a double
% --- Executes during object creation, after setting all properties.
T_m=[200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 360 370 380];
h_m=[199.97 209.97 219.97 230.02 240.02 250.05 260.09 270.11 280.13 285.13 290.16 295.17 298.18 300.19 305.22 310.24 315.27 320.35 350.49 360.58 370.67 380.77];
P_r_m=[.3363 .3987 .4690 5477 .6355 .7329 .8405 .9590 1.0889 1.1584 1.2311 1.3068 1.3543 1.3860 1.4686 1.5546 1.6442 1.7375 1.8345 1.9352 2.149 2.379 2.626 2.892 3.176];
v_r_m=[1707.0 1512.0 1346.0 1205.0 1084.0 979.0 887.8 808.0 738.0 706.1 676.1 647.9 631.9 621.2 596.0 572.3 549.8 528.6 508.4 489.4 454.1 422.2 393.4 367.2 343.2];
n=0;
v_r_v=get(handles.edit_v_r, 'string')
v_r_v=str2double(v_r_v)
handles.V_r=str2double(V_r)
f=ismember(V_r,handles.V_r_m);
for i=1:length(V_r_m)
V_rvalm=handles.V_r_m(i);
if f==0 && v_r_v>V_rval
i=n+1;
n=n+1;
v_rminidx=i;
v_rminval=v_r_m(n);
v_rmaxidx=i+1;
v_rmaxval=v_r_m(n+1);
end
end
handles.T=T_m(v_rminidx)+((v_r_v-v_rminval)/(v_rmaxval-v_rminval))*(T_m(v_rmaxidx)-T_m(v_rminidx));
handles.h=h_m(v_rminidx)+((v_r_v-v_rminval)/(v_rmaxval-v_rminval))*(h_m(v_rmaxidx)-h_m(v_rminidx));
handles.P_r=P_r_m(v_rminidx)+((v_r_v-v_rminval)/(v_rmaxval-v_rminval))*(P_r_m(v_rmaxidx)-P_r_m(v_rminidx));
set(handles.edit_T,'string',handles.T);
set(handles.edit_h,'string',handles.T);
set(handles.edit_P_r,'string',handles.T);
set(handles.edit_T,'string',handles.T);
guidata(hObject,handles)
function edit_v_r_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_v_r (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_T_Callback(hObject, eventdata, handles)
% hObject handle to edit_T (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_T as text
% str2double(get(hObject,'String')) returns contents of edit_T as a double
% --- Executes during object creation, after setting all properties.
n=0;
T_d=get(handles.edit_T,'String');
T_d=str2double(T_d);
f=ismember(T_d,handles.T_m);
for i=1:length(handles.T_m)
Tval=handles.T_m(i);
if f==0 && T_d>Tval
i=n+1;
n=n+1;
Tminidx=i;
Tminval=handles.T_m(n);
Tmaxidx=i+1;
Tmaxval=handles.T_m(n+1);
end
end
handles.h=handles.h_m(Tminidx)+((T_d-Tminval)/(Tmaxval-Tminval))*(handles.h_m(Tmaxidx)-handles.h_m(Tminidx));
handles.P_r_m=handles.P_r_m(Tminidx)+((T_d-Tminval)/(Tmaxval-Tminval))*(handles.P_r_m(Tmaxidx)-handles.P_r_m(Tminidx));
handles.v_r_m=handles.v_r_m(Tminidx)+((T_d-Tminval)/(Tmaxval-Tminval))*(handles.v_r_m(Tmaxidx)-handles.v_r_m(Tminidx));
set(handles.edit_v_r,'string',handles.v_r_m);
set(handles.edit_h,'string',handles.h);
set(handles.edit_P_r,'string',handles.P_r_m);
guidata(hObject,handles)
function edit_T_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_T (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in edit_radiobutton.
function edit_radiobutton_Callback(hObject, eventdata, handles)
% hObject handle to edit_radiobutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of edit_radiobutton
% --- Executes on button press in edit_pushbutton.
eng=get(handles.edit_radiobutton)
handles.eng=eng
function edit_pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to edit_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Clear=get(handles.edit_pushbutton,'value');
if Clear==1;
handles.T=[ ];
handles.h=[ ];
handles.P_r=[ ];
handles.v_r=[ ];
set(handles.edit_T,'string',handles.T);
set(handles.edit_h,'string',handles.h)
set(handles.edit_P_r,'string',handles.P_r);
set(handles.edit_v_r,'string',handles.v_r);
end
  1 件のコメント
Tyde Hilderbrandt
Tyde Hilderbrandt 2021 年 12 月 4 日
編集済み: Tyde Hilderbrandt 2021 年 12 月 4 日
I am now running into an issue where my radiobutton dose nothing, I'm guessing that once it pushed the GUI is not updateing. I have updated the posted code to include guidata(hobject,handles) to the radiobutton callback though this has not fixed the issue.

サインインしてコメントする。

採用された回答

VBBV
VBBV 2021 年 12 月 4 日
clear i j n
guidata(hObject,handles)
clear the loop counter variables used inside the callback function every time the GUI object is accessed. Try this approach
  2 件のコメント
VBBV
VBBV 2021 年 12 月 4 日
This would reset the variables back to initial values
Tyde Hilderbrandt
Tyde Hilderbrandt 2021 年 12 月 4 日
編集済み: Tyde Hilderbrandt 2021 年 12 月 4 日
This plus getting rid of the repetative n+1 seems to have worked.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by