Matlab takes in wrong data in command window

[fname, pname] = uigetfile('*.csv'); %ask for file
filename= fullfile(pname, fname); %file as string
assert(exist(filename, 'file')==2,'%s does not exist.', filename); %check if file exist
data = csvread(filename, 1) % reads data from file skipping headers
***************************Excel Data***************
(just need to shift values to according heading, they are in order)(there are like 55,000 rows but these are the first couple)
Latitude Longitude Elevation (metres) Distance (KMs) Gradient
30.6183 -96.33631 103.7 0 0
30.61832 -96.33629 103.6 0.003 0
30.61845 -96.3361 102.8 0.026 0
30.61845 -96.33608 102.7 0.028 0
**************************Data from Matlab************************
0.0363 -0.1203 0.1592 2.7105 0
0.0363 -0.1203 0.1595 2.7105 0
0.0363 -0.1203 0.1595 2.7106 0
0.0363 -0.1203 0.1578 2.7106 -0.0010
0.0363 -0.1203 0.1571 2.7107 -0.0020

5 commentaires

dpb
dpb le 12 Juin 2017
As I commented in the duplicate of this question already, need to see the actual file--attaching a short section of it would be ideal.
I've never had Matlab not correctly read a variable if it were properly formatted and the correct format string, etc., used to read it so there's something going on in the file we're not seeing.
Or, as mentioned there as well, perhaps you're not opening the file you think you are???
hi  hey
hi hey le 12 Juin 2017
Here is a picture. I just looked further into the program and when I click in the workspace it shows the correct information but it is not displaying the right information in the command space
hi  hey
hi hey le 12 Juin 2017
Here
Image Analyst
Image Analyst le 12 Juin 2017
Modifié(e) : Image Analyst le 12 Juin 2017
He meant to attach your CSV file, NOT a screenshot. Please attach a CSV file that demonstrates the problem so we can replicate it.
What's showing in the Excel screenshot is not the kind of CSV file that MATLAB likes. It should be just numbers, not numbers and text with some rows of numbers having different numbers of columns than other rows.
hi  hey
hi hey le 12 Juin 2017
I have matlab skip over the headers.

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 13 Juin 2017

1 vote

The data from MATLAB is the same, it's just that when you echo it to the command window, the data is multiplied by 1e-3. If you use fprintf() instead of just letting it automatically echo to the screen, you'll see they are the same (I tried it).

11 commentaires

dpb
dpb le 13 Juin 2017
Actually you're right IA--I thought I'd checked and there wasn't the right value...wonder what triggers the difference in the command window display between reading with csvread and textread? I've not noticed that behavior previously...altho it surely is as been shown this thread repeatable...
I don't know. Sometimes it just automatically decides to convert numbers spewed out to the command window to exponential notation. Perhaps you can avoid it by issuing the command
format long g
dpb
dpb le 13 Juin 2017
If I get a chance't I'll look a little--wonder if there's a possibility of a floating-pt rounding difference between two competing i/o routines that triggers just enough of a numerical difference to trigger the display logic...peculiar.
I'll just delete my above response--it tends to obfuscate the real issue.
dpb
dpb le 13 Juin 2017
Modifié(e) : dpb le 13 Juin 2017
Well, that isn't it (and I thought would be really bizarre if so)--
>> d=textread('route.csv','','headerlines',1,'delimiter',',');
>> dcsv=csvread('route.csv',1);
>> all(d(:)==dcsv(:))
ans =
1
>>
But switching back and forth which function is called triggers the change in command window presentation repeatably...
csvread calls dlmread which is just a wrapper for textscan while textread is implemented in its own venerable mex-file so I thought perhaps that might have a rounding one-bit fp difference or the like that was just enough.
Must be something else in internal state going on; where's Yair when you need him? VBG
hi  hey
hi hey le 13 Juin 2017
So what should I do? The data in the workspace is correct but the output data is incorrect. Would that change the output of my calculations?
Image Analyst
Image Analyst le 13 Juin 2017
It's not incorrect. It's just scrolling the part that says x1e-3 past the top of the command window when you echo it there. So it's only the display that looks different. Just try it, the numbers in your calculations will be correct.
hi  hey
hi hey le 13 Juin 2017
this is the first row from excel
30.6183 -96.33631 103.7 0 0
this is the first row from matlab
0.0363 -0.1203 0.1592 2.7105 0
The numbers are completely off for me. What are the rows that you get back from matlab and excel?
Walter Roberson
Walter Roberson le 13 Juin 2017
That "first row from matlab" is not the first row: the rest of the data has scrolled off the top of the screen.
>> csvread('routeshort.csv',1)
ans =
1.0e+03 *
0.0306 -0.0963 0.1037 0 0
0.0306 -0.0963 0.1036 0.0000 0
0.0306 -0.0963 0.1028 0.0000 0
0.0306 -0.0963 0.1027 0.0000 0
0.0306 -0.0963 0.1024 0.0000 0
0.0306 -0.0963 0.1018 0.0000 0
0.0306 -0.0963 0.1017 0.0000 -0.0040
0.0306 -0.0963 0.1017 0.0001 -0.0040
0.0306 -0.0963 0.1016 0.0001 -0.0040
...
>> d=textread('route.csv','','headerlines',1,'delimiter',',');
>> d(1:5,:)
ans =
30.6183 -96.3363 103.7000 0 0
30.6183 -96.3363 103.6000 0.0030 0
30.6184 -96.3361 102.8000 0.0260 0
30.6184 -96.3361 102.7000 0.0280 0
30.6184 -96.3361 102.4000 0.0310 0
>>
I'd revert to the previous comment re: csvread and needing to also see precisely what you've done in code in command line...I've got to think you're just not actually comparing the same files, though, somehow, but it's not easy when don't have access to the workstation to be able to see just what, specifically, might be the issue.
hi  hey
hi hey le 13 Juin 2017
Modifié(e) : hi hey le 13 Juin 2017
Yep,I found it. It was line 46885 in matlab. Ok thank you all for helping me!
format shortG at the top and the textread fixed the issue
Image Analyst
Image Analyst le 13 Juin 2017
You can see in dpb's post that both csvread() and textread() have the same numbers, they're just formatted differently in the display. csvread has the 1.0e+03 in there whereas text read doesn't. But internally, in your code, using the arrays from either function will give you the same result. If you echo to the command line, the 1.0e+03 just scrolled off the top of the command window, like Walter and I said, but it's still the same numbers. So I'd use csvread() (if you have a recent version where you can tell it to skip a header line) since textread() is deprecated.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by