MATLAB Struct contents reference from a non-struct array object

  • Thread starter Thread starter quasarLie
  • Start date Start date
  • Tags Tags
    Array Reference
Click For Summary
The discussion revolves around a coding issue related to plotting data from a file named 'bic.dat' in MATLAB. The user encounters an error when trying to reference struct contents from a non-struct array, which is resolved by correctly indexing the data as arrays. They seek to plot two columns of data and overlay a regression line defined by the equation g2 = 0.97*r + 0.39. Confusion arises regarding the correct plotting of errors and the regression line, with suggestions made to clarify the intended output. Ultimately, the user is advised to ensure that the plots are on the same scale and to separate the error calculations from the regression line plotting.
quasarLie
Messages
50
Reaction score
0
Hello,
i need help with my code please , i have a file named 'bic' which contain only 2 columns . I want to plot this two columns.
Matlab:
Data=importdata('bic.dat');
g=Data.data(:,2)
r=Data.data(:,1);
delta=r-g;

plot(r,delta,'b*');
And i have this error
struct contents reference from a non-struct array object.
A sample of my columns
20.88 20.391
19.624 19.661
19.218 19.313
20.158 19.689
20.759 20.348
20.30 19.789
20.666 20.813
19.927 19.696
21.139 20.162
20.39 20.568
20.386 20.552
21.112 21.096
20.13 20.193
19.497 19.354
20.851 20.795
19.505 19.534
20.253 20.634
20.555 20.176
20.94 21.022
20.495 20.101
20.748 20.499
19.6 18.691
20.377 20.026
20.017 19.610
Thanks
 
Physics news on Phys.org
importdata does not make a structure. It makes an array. Use " g = Data(:,2)" and " r = Data(:,1)"
 
thanks a lot, do you know how can I use this equation in the code
g=0.97*r+0.39
 
quasarLie said:
thanks a lot, do you know how can I use this equation in the code
g=0.97*r+0.39
I don't understand. Use it for what? If that is a regression equation, then you can add something like this to determine the errors for each point of g:
Code:
g2=0.97*r+0.39
err = g - g2
 
Yes it s a linear regression. i tried what you said but it does not work.
I did this code for the same file to plot the two columns but i don t know how to use the equation
Matlab:
fidi = fopen('bic.dat');
t = textscan(fidi, '%f%f', 'Delimiter',',');
fclose(fidi);
x=t{1};
y=t{2};
delta= x-y;
figure(1)
plot(y, delta, 'k*')
xlabel('\bfg')
ylabel('\bfr-g')
 
The code I suggested executes for me at the end of the code below. That code is the correction of your original post. But it's not clear to me what this is supposed to be doing. Is g the original data and r the regression estimates? I can not help if I have to guess at what you have already done and at what you want to do. All I can do is suggest (possibly meaningless) code that will not abort.

Code:
Data=importdata('bic.dat');
g=Data(:,2)
r=Data(:,1);
delta=r-g;

plot(r,delta,'b*');
g2=0.97*r+0.39;
err = g-g2
 
Yes I tried it for this code.
I want the equation of the line in form g2=0.97*r+0.39 to appear on my scatter plot
Thanks for your help
 
You can do multiple plots in one plot command as the examples of @DrClaude 's link shows or you can hold a plot and use other plot commands.

I suspect that you want something like this:
Code:
Data=importdata('bic.dat');
g=Data(:,2)
r=Data(:,1);

hold off;
plot(r,g,'b*');
hold on;
g2=0.97*r+0.39;
plot(r,g2);
 
  • #10
upload_2018-4-25_15-46-48.png

With the equation the code give me this, and it s not correct
upload_2018-4-25_15-47-38.png
 

Attachments

  • upload_2018-4-25_15-46-48.png
    upload_2018-4-25_15-46-48.png
    4.9 KB · Views: 1,228
  • upload_2018-4-25_15-47-38.png
    upload_2018-4-25_15-47-38.png
    3.5 KB · Views: 1,180
  • #11
and i want something like this
upload_2018-4-25_15-50-57.png

This is the code that i am using
Matlab:
fidi = fopen('bic.dat');
t = textscan(fidi, '%f%f', 'Delimiter',',');
fclose(fidi);
x=t{1};
y=t{2};
delta= x-y;figure(1)
plot(y, delta, 'k*')

y2=0.97*x+0.39
hold on;
plot(x,y2,'red')
I tried with yours and it s the same problem
 

Attachments

  • upload_2018-4-25_15-50-57.png
    upload_2018-4-25_15-50-57.png
    9.1 KB · Views: 1,194
  • #12
Yes. You are mixing the plot of the errors, delta, with the plot of the estimator, y2. They are not on the same scale. The red line on the plot that you say you want is just the x axis. There is no need to plot y2.

If I am wrong about this, then it is because I still don't understand what you are trying to do and can not help you.