Struct contents reference from a non-struct array object

  • Context: MATLAB 
  • Thread starter Thread starter quasarLie
  • Start date Start date
  • Tags Tags
    Array Reference
Click For Summary

Discussion Overview

The discussion revolves around a coding issue in MATLAB related to plotting data from a file and incorporating a regression equation into the plot. Participants are trying to resolve an error message and clarify how to correctly implement and visualize the regression line alongside the data points.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant encounters an error regarding "struct contents reference from a non-struct array object" when attempting to plot data imported from a file.
  • Another participant suggests that the function importdata returns an array, not a structure, and recommends using g = Data(:,2) and r = Data(:,1).
  • A participant inquires about incorporating a regression equation g=0.97*r+0.39 into their code.
  • There is a suggestion to calculate errors based on the regression equation, but the application of this suggestion is unclear to some participants.
  • One participant shares a different approach using textscan to read the data and plot it, but expresses uncertainty about how to integrate the regression equation.
  • Another participant mentions that the code provided executes correctly but questions the purpose of the variables g and r.
  • There is a request to display the regression equation on the scatter plot, with multiple suggestions on how to achieve this.
  • One participant points out that mixing plots of errors with the regression line may lead to incorrect visual representation, indicating a potential misunderstanding of the intended output.

Areas of Agreement / Disagreement

Participants express differing views on how to correctly implement the regression line in the plot and whether the current approach is appropriate. There is no consensus on the best method to visualize the data and regression together.

Contextual Notes

Some participants express confusion regarding the intended outcome of the code, highlighting a lack of clarity in the problem statement and the relationship between the variables involved.

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,244
  • upload_2018-4-25_15-47-38.png
    upload_2018-4-25_15-47-38.png
    3.5 KB · Views: 1,200
  • #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,213
  • #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.