-1

I am using matlab Guide. I want to take a string that I enter in an Edit text Box, and convert it into a vector of numbers so that I can plot a graph from the vector. Here is the code I wrote for converting the String into A vector of numbers:

function value = substrings (a)
j = 1;
word = a;
for i = 1:length(word)
    if word(i)~= ' '

        q(1,j) = str2double(word(1,i));
        j = j+1;
    end
end
value = q;

end

This Code eliminates spaces so if I enter '1 2 3 4 5' It wil become [1 2 3 4 5] The problem i have is i dont know how to include this in my main code so that i can input the string in the edit text box and the send it to a button to plot it.

here is the section in the Guide:

function text_Callback(hObject, eventdata, handles)
% hObject    handle to text (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
word = get(hObject,'String');

for i = 1:length(word)
    if word(i)~= ' '

        q(1,k) = str2double(word(1,i));
        k = k+1;
    end
end
handles.To_Plot = q;

im going to plot handles.To_Plot with the button.

1 Answers1

0

Hmm, I'm not quite sure what you're asking, but I'm confident I can help you if you provide clarification. Here's what I think you want:

There is no need to "send it" to anything. You just need to use guidata(hObject, handles) to sync your handles variable, then place your plot commands in the button press callback.

function button_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to text (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

doSomethingWith(handles.To_Plot);

end

If this isn't what you need just post some clarification to your question, and I'll try to help. Tell me about the specific output you expect, and how what you're seeing is different.

Justin Fletcher
  • 2,319
  • 2
  • 17
  • 32
  • Hey thanks for replying, i guess my wording was a bit confusing il try my best to clarify. basically when i enter text into the edit text box, that text will be received as a string. therefore if i enter 1 2 3 4 5 a string will form '1 2 3 4 5'. i want to take that data and plot it, but i cant plot a string. so i need to split it up into intergers. thats what my function i wrote does. problem is that i cant get that function to work in my main, it only works in isolation. – user3637460 Jul 21 '14 at 20:43
  • Hmm, well that doesn't help too much. What do you mean that it doesn't work in your main function? So you're saying that you can put substring(a) in your main function and it won't work, but it does work from the console? Please edit your question and add the error message. – Justin Fletcher Jul 21 '14 at 21:01