Struct contents reference from a non-struct array object

In summary, the conversation revolves around a coding issue where the individual is trying to plot two columns of data from a file named 'bic'. They are trying to use a linear regression equation of the form g2=0.97*r+0.39 to appear on the scatter plot. However, the code is not working as expected and they are seeking help from an expert.
  • #1
quasarLie
51
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
  • #2
importdata does not make a structure. It makes an array. Use " g = Data(:,2)" and " r = Data(:,1)"
 
  • #3
thanks a lot, do you know how can I use this equation in the code
g=0.97*r+0.39
 
  • #4
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
 
  • #5
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')
 
  • #6
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
 
  • #7
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
 
  • #9
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,137
  • upload_2018-4-25_15-47-38.png
    upload_2018-4-25_15-47-38.png
    3.5 KB · Views: 1,116
  • #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,119
  • #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.
 

What does the error message "Struct contents reference from a non-struct array object" mean?

This error message means that you are trying to access a field or property of a struct from an array object that is not a struct. In other words, you are trying to use a struct-specific function or operation on a non-struct data type.

Why am I getting the error "Struct contents reference from a non-struct array object"?

You are getting this error because you are attempting to use a struct-specific function or operation on a non-struct data type. This is not allowed as the function or operation is only meant to be used on struct data types.

How can I fix the "Struct contents reference from a non-struct array object" error?

To fix this error, make sure that you are using the correct function or operation for the data type you are working with. If you are working with an array object, make sure to use functions and operations that are meant for arrays and not for structs.

Can I use struct-specific functions on non-struct data types?

No, you cannot use struct-specific functions or operations on non-struct data types. These functions and operations are only meant to be used on struct data types and will result in the "Struct contents reference from a non-struct array object" error if used on other data types.

Is there a way to access struct fields from non-struct data types without getting the "Struct contents reference from a non-struct array object" error?

No, you cannot access struct fields from non-struct data types. Struct fields are only accessible from struct data types and attempting to access them from non-struct data types will result in the "Struct contents reference from a non-struct array object" error.

Back
Top