How to replace certain values in an existing array based off of inputdlg?

In this gui I have been coding, I generate some m x n dimensional array with a function I have written. What I want to know is: how can I replace certain values in this array from another function? I am using the GUIDE GUI program with callbacks.
I want to take user input from a dialog box and assign those values to variables for use in an equation I have written.
function edit_button_Callback(hObject, eventdata, handles)
% hObject handle to edit_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
prompt ={'Param1' 'Param2' 'Param3' 'Param4' 'Param5' 'Location'};
dlg_title = 'Input';
num_lines = 1;
def = {'vectorinput1','vectorinput2','vinput3','vinput4','vinput5','index'};
def = def';
deff = cellstr(def);
answer = inputdlg(prompt, dlg_title, num_lines, deff);
[Rsh, Rs, Isc, Voc, n, index] = answer{:};
That also doesn't seem to be working at the moment. Anyway, after I have the user input values for the parameters, I would use the equation I have written to generate a column array and a row array. How would I be able to change the values in an already existing array based on the location specified in the inputdlg?

 Accepted Answer

Hi Roel. Try this code:
Rsh = str2num(char(answer{1}));
Rs = str2num(char(answer{2}));
Isc = str2num(char(answer{3}));
Voc = str2num(char(answer{4}));
n = str2num(char(answer{5}));
index=str2num(char(answer{6}));
and then look at the attached files below (a simple example which shows how you can pass your data in GUI).
Hope this helps.

1 Comment

This helps so much! I was struggling to save things to a variable for use later. Thanks!

Sign in to comment.

More Answers (1)

i think that last line should be
[Rsh, Rs, Isc, Voc, n, index] = deal(answer{:});
for it to split answer into the different variables.
So if i understand the rest of your question correct, you have a mxn array. Lets call that X and based on what [Rsh, Rs, Isc, Voc, n, index] (perhapse n and index?). you want to replace it with something else. then you can go
X(n,index) or something along that line to specify the row and column intersection. such that if i go:
X = [1 2 3;4 5 6;7 8 9];
X(2,3) = 1;
then the second row third column would then be replaced with 1.

10 Comments

I realize now that the "n" can be misleading. The index does correspond to that array but "n" is its own variable independent of the array. The first part does solve my first problem though. For the X(2,3) line instead of a 1, how can I replace it with the output of some other function?
I have attached the script so you can know more or less what's going on. Just pay attention to the last two functions.
I don't see where you're trying to perform the substitution. is the substitution going on in the edit_button callback? or in another callback.
I haven't implemented it yet but it would be going in the edit_button callback. I would take the user inputs from inputdlg and run the CalculationIVRsRsh equation like in the plot_button callback. Then take this output and replace the corresponding elements in the earlier array (I4 and V4). Then plot the change
well, since it would take a lot of time i don't really have right now to understand the things that's going on and describe what you fully need to implement the substitution I'll just talk vaugely.
Just like your CalculationIVRsRsh callback in the plot_button you have
for ind=1:taille
[I(:,ind), V(:,ind)]=CalculationIVRsRsh(Isc(ind),Voc(ind),T,n(ind),Rs(ind),Rsh(ind));
end
which is basically what i showed you above in the X(2,3) =1 case. such that if we just take those lines you see that in the for loop you are actually building the I and V arrays index by index. However since i show you can specify the ind, you can directly index whatever you want. I think Amir's example below should be a good example of how to pass the original variable calculated from the plot callback. (i haven't look at his example but its probably good).
I figured it out! Amir's example helped, thank you!
One last question, will this method work for an input like X(1:3,3)?
Roel Yes you can do that also. You need to add some other controllers. 1) Add a radio button asking the user for :is it a range? (Yes/NO) 2) Add another text box which is for the last point of your data range. 3) If the user's selection (in radio button) is not "Yes", you can disable the second text box (step 2) which is for the range. use this:
set(handles.edit, 'Enable', 'off')
You can also code to your static texts to be changed based on you radio button value. If this couldn't help you please let me know then I can attach a simple example for you. And the last point, try this code:
A='2+3';
A
B=eval(A)
If your app's users are familiar with Matlab's syntax you can use eval. I mean you can evaluate user's string (which has been typed in text box, for example '[1:3]') and convert it to meaningful syntax which can be run in Matlab. Here is an example
A=[1 2 3 4 5 6 7 8 9 10];
B='3:5';
% B is a string variable similar to what you get from textbox in gui
A(eval(B))
I am not quite certain on the static texts corresponding to a radio button. Can I make a radio button appear on an input dlg? Or rather, can I call this GUI from my Main GUI? In other words can I run one GUI, then upon some callback, a new one will open?

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Asked:

on 13 Aug 2014

Commented:

on 14 Aug 2014

Community Treasure Hunt

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

Start Hunting!