Matlab App Designer: Index exceeds matrix dimensions error
1 ビュー (過去 30 日間)
Chandler Clark 2018 年 8 月 17 日
I'm trying to take 4 sensor signals from Arduino to Matlab. The signals are read until the user decides to end the process (a button on the App). The signal comes into Matlab as a string where it is then split (0.1,0.2,0.4,0.5,1.2 to 0.1 0.2 0.4 0.5 1.2).
I'll try to condense the code to just one signal. I keep getting the "Index exceeds matrix dimensions" error. My code is also running very slow, so any tips on this would be greatly appreciated.
My Arduino code is fine. It's running as a switch case: when 's' is sent, the signal is read; when 'd' is sent, the program jumps out of that Arduino loop and no longer sends the signal. Without Matlab, it outputs exactly what I need it to.
Programs: MATLAB R2017a, App Designer, Arduino 1.8.2
Matlab App Code:
char s; %Send to Arduino to start readings
counter = 1;
char d; %Send to Arduino to stop readings
app.EndTest = 0;
app.S1 = zeros(1, app.timer); %app.timer is set by user
array1 = zeros(1, app.timer); %I tried to preallocate app.S1 and array1 to speed up the code
while app.EndTest == 0
data = fscanf(app.s, '%s'); %Gives all data in string
IncomingString = split(data, ','); %Splits the string
app.S1 = IncomingString(1); %Load Cell 1 is set to the first column values
arrayString1 = str2double(app.S1); %Converts the ordinary array to a double
app.S1EditField.Value = arrayString1; %Print Signal 1 value
S1Time = arrayString1(counter); %This is where the index exceeds dimensions error comes up
app.realGraph = plot(app.RealTimeAxes,timeData, arrayString1, 'b'); %Graph the result
counter = counter + 1;
fprintf(app.s, 'd'); %Sends 'd' to Arduino to stop reading
I have this code for 4 sensors (basically it just repeats everything four times). I know this code will not be able to copy and paste because it's in App Designer, but if you see anything that could help that would be awesome.
Thanks ahead of time!
Tyler Kirsten 2018 年 8 月 18 日
Funny I'm having a very similar problem, doing basically the same thing, reading in sensor data from device over UDP and converting and storing it in an array for future access. thing is I also declared my array as you did with receiveValues = zeros(1, 7). And I manually index each element last being receiveValues(7), but still get the same error you do. I'm running a few timers though calling functions so app basically freezes each time. If you come up with something let me know and I'll do the same. Good luck!!
回答 (1 件)
Freya H 2018 年 8 月 20 日
What format/size does the arrayString1 variable have?
My best guess would be that in "app.S1 = IncomingString(1);" you possibly select a single string and then convert this to double which would end up in arrayString1 being a single value and not an array.
Dennis 2018 年 8 月 21 日
arrayString(1) should not be a problem, Matlab has no problem in returning the first entry of a scalar. However accessing arrayString(2) will throw an error.
A few questions from my side:
- What is S1time? It appears that you are not using it, even if you could increment arrayString(counter) you would overwrite S1time in every iteration.
- You are sending 'stop reading' in every loop iteration, but your start reading is outside the loop, this seems a bit odd to me.
- What is the exit condition of your loop? app.EndTest never gets updated - so it will run infinite (or until it throws an error)
- You preallocate app.S1 (i think this might cause an error, because appdesigner does not want you to append things to app?), but then you overwrite app.S1 inside your loop
- You preallocate array1 and never use it
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!