importing data using function change the colomns order

1 回表示 (過去 30 日間)
Samaneh Arzpeima
Samaneh Arzpeima 2020 年 4 月 13 日
コメント済み: Samaneh Arzpeima 2020 年 4 月 15 日
I have this file
strikeLdep5.dat
I want to read this file using function "import_sitefile.m" and plot it.but after reading the file , colomns order get change!
I can't understand why!! could you please tell me why?
the code to plot the data:
clc
clear
i=5
filename = sprintf('strikeLdep%d.dat',i);
data = import_sitefile(filename, 22, 2521);
x = abs(data(:,8))
y = abs(data(:,4))
z = linspace(1,10,length(x));
scatter(x,y,[],z,'filled')
xlabel('Normal Stress')
ylabel('Shear Stress')
colormap(jet)
hc=colorbar('eastoutside');
LabelText = 'time';
box on
xlim([49 52])
ylim([24 36])
title(sprintf('site%d',i))
saveas(gcf,sprintf('siteL%d.png',i))

採用された回答

dpb
dpb 2020 年 4 月 13 日
編集済み: dpb 2020 年 4 月 13 日
Worked ok here...
>> data=import_sitefile('strikeLdep5.dat',22);
>> whos data
Name Size Bytes Class Attributes
data 2500x8 160000 double
>> data(1:10,:)
ans =
0.0100 0 0.0000 25.1000 0 0 -0.0000 -50.0000
0.0200 -0.0000 -0.0000 25.1000 0.0000 0.0000 0.0000 -50.0000
0.0300 -0.0000 0.0000 25.1000 0.0000 0.0000 0.0000 -50.0000
0.0400 0.0000 0.0000 25.1000 -0.0000 -0.0000 0.0000 -50.0000
0.0500 0.0000 0.0000 25.1000 -0.0000 0.0000 -0.0000 -50.0000
0.0600 0.0000 0.0000 25.1000 -0.0000 0.0000 -0.0000 -50.0000
0.0700 -0.0000 -0.0000 25.1000 0.0000 -0.0000 0.0000 -50.0000
0.0800 -0.0000 -0.0000 25.1000 0.0000 -0.0000 0.0000 -50.0000
0.0900 -0.0000 -0.0000 25.1000 0.0000 -0.0000 0.0000 -50.0000
0.1000 0.0000 0.0000 25.1000 -0.0000 -0.0000 -0.0000 -50.0000
>> format longe; format compact
>> data(1:10,2)
ans =
0
-5.629038000000000e-34
-8.132045000000000e-32
4.125600000000000e-29
1.748054000000000e-27
1.593390000000000e-26
-1.051301000000000e-26
-6.447970000000000e-25
-2.170367000000000e-24
3.242644000000000e-24
>>
However, you don't need any special routine to read the data file and the function import_sitefile is very inefficient as it contains the following code:
for block=2:length(startRow)
frewind(fileID);
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, ...
'Delimiter', '', ...
'WhiteSpace', '', ...
'TextType', 'string', ...
'EmptyValue', NaN, ...
'HeaderLines', startRow(block)-1, ...
'ReturnOnError', false, ...
'EndOfLine', '\r\n');
for col=1:length(dataArray)
dataArray{col} = [dataArray{col};dataArrayBlock{col}];
end
end
that first rewinds the file after every record and then has to reskip every line as a header line until it gets to the next record.
On top of that, it does dynamic reallocation by concatenating the new data on the old for every column after every record with no preallocation at all.
The format string is also specific for 8 and only 8 columns (which is probably ok since it's specific for the given file format, but also uses a fixed field width to mimic reading a fixedwidth file. That's ok but not necessary as the file is complete and can be presumed to always be so as it clearly is machine-generated.
Use one of the more modern input functions -- readmatrix is probably the preferred/recommended by TMW; or readtable if a table instead of an array were the desired result. While has been deprecated, I still like and use the venerable textread quite a bit for such things as it also doesn't need an input format string for regular data nor the messing around with fopen/fclose for textscan...
For comparison,
data1=textread('strikeLdep5.dat','','headerlines',21);
>> all(all(data==data1))
ans =
logical
1
>> data2=readmatrix('strikeLdep5.dat','numheaderlines',21);
>> all(all(data==data2))
ans =
logical
1
>> fid=fopen('strikeLdep5.dat','r');
>> data0=cell2mat(textscan(fid,repmat('%f',1,8),inf,'HeaderLines',21,'CollectOutput',1));
>> fid=fclose(fid);
>> all(all(data==data0))
ans =
logical
1
>>
All return same data identically; a brief timing here showed that actually the import_sitefile function didn't fare nearly as badly as I would have thought and readmatrix was a dog, performance-wise...
textscan Elapsed time is 0.012087 seconds.
import_sitefile Elapsed time is 0.018422 seconds.
textread Elapsed time is 0.035900 seconds.
readmatrix Elapsed time is 0.268663 seconds.
  5 件のコメント
dpb
dpb 2020 年 4 月 14 日
Well, actually when go back and look at your plotting code as compared to the data and to the file, it appears you have the right columns for the way you labelled the x- and y-axes--just that the magnitude of the data in this particular file doesn't match what you apparently were expecting.
>> [min(data(:,8)) max(data(:,8));min(data(:,4)) max(data(:,4))]
ans =
-50.0361 -49.7762
25.0500 25.4598
>>
but you set
xlim([49 52])
ylim([24 36])
so that the x data are way over in left field at -50 while you're displaying the axis at +50. You've also compressed the y axis drastically as compared to the range of variable values in the file.
Try just leaving off those two lines and let the plot auto-range and you end up with an interesting plot.
(I don't have any idea what it means, but it is interesting... :) )
Samaneh Arzpeima
Samaneh Arzpeima 2020 年 4 月 15 日
Thank you so much for reviwing my files.got it.Thanx again

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Identification についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by