I had to update MATLAB recently from 2008 to 2014.
MATLAB's importdata no longer outputs just an array of useable values if there's any non-number text in the file. Testing shows that if I remove all my comments from my file, importdata returns the required data.
I tried something like this
structure = importdata('filename.txt')
structure.data
but my first line, which has a comment at the end of line (and thus non-number text) gets cut off. I have a bunch of comments throughout my data files and I'd rather not have to remove all my comments.
This answer seems out of date.
Is textscan the only way to fix this?
Data file I've been working with.
% Vin: 5 MHz 6.5 mV pk-pk
% ADRF: Pre: 6 dB, Filt: 31 MHz, VGA: 28 dB, Post: 12 dB
% VGain Vin Vout
0 6.51 4.55 % Dirty input
40 6.52 4.57
70 6.54 4.60
110 6.55 4.88
160 6.54 6.21
200 6.53 7.83
240 6.54 10.36
270 6.53 12.95
320 6.53 18.10
360 6.52 24.70
400 6.52 32.20
440 6.51 44.60
480 6.51 57.90
520 6.52 79.50
560 6.51 105.3
600 6.53 147.9
640 6.54 195
680 6.53 272
720 6.51 357
760 6.50 500
800 6.50 677
840 6.47 881
880 6.47 993
920 6.47 1012
960 6.47 1012
1000 6.47 1012
Is there any reason why you don't want to use textscan? This works for me in Matlab 2010 and 2013:
fid=fopen('testdata.dat');
data=textscan(fid,'%f %f %f','Headerlines',3,'Commentstyle','%');
fclose(fid);
data=cell2mat(data);
EDIT:
As long as you don't have a comment in the first line of your data, importdata('testdata.dat') should work fine. There seems to be a change in the way the number of headerlines is determined between the Matlab versions you are comparing. If you prefer importdata to textscan, try this:
data=importdata('testdata.dat',' ',3)
then data.data should contain all your data and it is still quite readable.
Related
If I have a matrix with first and second columns related to coordinates and third one related to a value, how could I interpolate third column values creating iscurves and colorbar ranges?
M=[
342854 657145 309
342996 657287 73
343137 657428 84
342006 657145 1122
342147 657287 777
342289 657428 426
342430 657570 638
342571 657711 200
342713 657852 787
341723 657711 1141
341864 657852 555
342006 657994 1157
342147 658135 355
342289 658277 374
341299 658135 467
341440 658277 672
341582 658418 459
341723 658560 735
341864 658701 781
341016 658701 1233
341157 658842 218
341299 658984 539
341370 659054 1351];
and obtain something like the attached image
As your data is not in an uniform grid, you need to use griddata for interpolation.
[xq,yq]=meshgrid(linspace(min(M(:,1)),max(M(:,1)),100),linspace(min(M(:,2)),max(M(:,2)),100));
zq=griddata(M(:,1),M(:,2),M(:,3),xq(:),yq(:),'cubic'); %cubic for smoother results
[c,h]=contourf(xq,yq,reshape(zq,100,100));
clabel(c,h);
I'm plotting a scatter plot and would like to have the a-axis as entry named than index. My data is from MASS in R and looks like this
animals={'Mountain beaver';'Cow';'Grey wolf';'Goat';'Guinea pig';'Dipliodocus';'Asian elephant';'Donkey';'Horse';'Potar monkey';'Cat';'Giraffe';'Gorilla';'Human';'African elephant';'Triceratops';'Rhesus monkey';'Kangaroo';'Golden hamster';'Mouse';'Rabbit';'Sheep';'Jaguar';'Chimpanzee';'Rat';'Brachiosaurus';'Mole';'Pig'};
body=[1.35 465 36.33 27.66 1.04 11700 2547 187.1 521 10 3.3 529 207 62 6654 9400 6.8 35 0.12 0.023 2.5 55.5 100 52.16 0.28 87000 0.122 192];
brain=[8.1 423 119.5 115 5.5 50 4603 419 655 115 25.6 680 406 1320 5712 70 179 56 1 0.4 12.1 175 157 440 1.9 154.5 3 180];
% Plot
x=1:length(body);
scatter(x,body,'filled','d')
hold
scatter(x,brain,'filled')
legend('body', 'brain','location','east');
How could I amend the program so my scatterplot display animals in xticks with 45 degrees?
I think this is what you want:
% add these lines at the end of your code
set(gca,'XTickLabel',animals)
set(gca,'XTick',1:numel(animals));
xlim([0 numel(animals)+1]);
set(gca, 'XTickLabelRotation', 45);
which gives this:
I need to plot some trajectories with matlab, i have the coordinates of each points in a file .txt, i work with c++ i want to plot this trajectories with Matlab to make some comparisons, this is an example of the file who contains the coordinates :
515 // this is x
317 // this is y
0 // i dont want to import this variable
511 // this is x
328 // this is y
20 // i dont want to import this variable
508
353
40
511
... etc
there is a function in Matlab that can help me to import only x and y?
file :
172
489
54460
283
469
54480
388
428
54500
476
384
54520
555
350
54540
635
325
54560
700
286
54580
760
250
54600
811
222
54620
840
192
54640
856
171
54660
871
175
54680
890
181
54700
930
170
54720
979
168
54740
You can read in all values using textscan and simply ignore every third value in the output by using the * in the format specifier.
fid = fopen('filename.txt', 'r');
data = textscan(fid, '%d\n%d\n%*d\n');
[x,y] = data{:};
fclose(fid);
Another option is to read in all data, then reshape and grab the parts you care about.
fid = fopen('filename.txt', 'r');
data = textscan(fid, '%d');
data = reshape(data{1}, 3, []);
x = data(1,:);
y = data(2,:);
fclose(fid);
does anybody know why the function countourf doesn't use the color corresponding to the maximum value anywhere in the plot area?
if you try the code below, and then the command
get(h_colorbar,'YLim')
Matlab returns an upper limit which is not the highest element of the matrix (500), but a smaller number (475.9091).
As you can see from the commented lines in the code, I was able to change the upper/lower limits of the colorbar, but of course this doesn't solve the problem.
I'd like Matlab to actually use the highest values in my matrix of data; for instance, I'd like to see the point corresponding to (200,300) colored in the darkest red.
Xdata=[7 11 15 19 23 27 31 39 50 75 100 200];
Ydata=[50 100 140 150 200 300];
dataZ=[...
500 500 438 310 269 253 245 238 235 237 241 500 ...
500 414 291 259 248 244 241 239 239 250 274 500 ...
500 335 268 251 246 243 241 240 242 261 308 500 ...
500 323 264 250 245 243 241 241 243 265 319 500 ...
500 289 256 248 244 243 242 243 248 287 500 500 ...
360 264 250 245 244 243 244 247 261 376 500 500 ...
]';
% % % In matrix form
mdataZ=vec2mat(dataZ,length(Xdata));
[mXdata,mYdata]=meshgrid(Xdata,Ydata);
figure_5=figure;
set(gca,'FontName','Times New Roman', 'FontSize',16,'YColor','k')
hold on
box on
% % % set(gca,'CLim',[min(dataZ) max(dataZ)])
contourf(mXdata,mYdata,mdataZ,10)
scatter(19,140,50,'k')
h_colorbar=colorbar;
set(get(h_colorbar,'ylabel'),'string','Z','FontName','Times New Roman', 'FontSize',18)
set(h_colorbar,'FontName','Times New Roman','FontSize',16)
% % % set(h_colorbar,'YLim',[200 500],'YTick',[0:50:500])
% % % caxis([200 500])
axis([min(min(mXdata)),max(max(mXdata)),min(min(mYdata)),max(max(mYdata))])
xlabel('X','FontName','Times New Roman', 'FontSize',18)
ylabel('Y','FontName','Times New Roman', 'FontSize',18)
Any idea?
Thanks in advance!
contourf splits your data into n (10 for your case) levels. Unless you specify the levels they are chosen automatically by the function.
The highest level must be lower than the highest point in your data. Maybe it could be the same, I'm not sure how matlab treats values equal to a contour in this case. But if you leave it to automatic contour levels it will definitely be lower.
The individual data points are not plotted by the function, only the contour heights. Therefore, the value 500 is not in the colormap and the max is the height of the highest contour.
To solve this you can put a vector of contour values rather than n. Put the highest value close or equal to 500.
Following your suggestion, I replaced the line calling the countourf function.
Instead of the number of elements (10), I put linspace(min(dataZ),max(dataZ),10).
Interestingly, the minimum value used by contourf seems always to be the lowest element of the input matrix.
If you're curious, compare the previous code and the following:
new_figure=figure;
set(gca,'FontName','Times New Roman', 'FontSize',16,'YColor','k')
hold on
box on
contourf(mXdata,mYdata,mdataZ,linspace(min(dataZ),max(dataZ),10))
scatter(19,140,50,'k')
h_colorbar=colorbar;
set(get(h_colorbar,'ylabel'),'string','Z','FontName','Times New Roman', 'FontSize',18)
set(h_colorbar,'FontName','Times New Roman','FontSize',16)
axis([min(min(mXdata)),max(max(mXdata)),min(min(mYdata)),max(max(mYdata))])
xlabel('X','FontName','Times New Roman', 'FontSize',18)
ylabel('Y','FontName','Times New Roman', 'FontSize',18)
I had two arrays, (data1 is a header array of strings and data2 is the data array of numbers)
data1 = {'#','Area','C Xp','C Yp','Length','B #','R','L','Ch','E1 Xp','E1 Yp','E2 Xp','E2 Yp'};
data2 = [1 939 -397 586 99 2 2 0 -1 -450 588 -352 572
2 1185 -287 294 145 2 1 1 0 -317 359 -235 244
3 592 -242 486 77 3 2 1 0 -278 488 -202 477
4 818 -144 480 60 2 0 2 1 -181 488 -135 451
5 377 -23 -443 37 1 0 1 0 -42 -459 -12 -460
6 923 32 -234 67 1 0 0 0 -3 -260 60 -212
7 812 150 -148 54 1 0 1 0 136 -130 169 -161
8 5968 428 432 402 3 3 0 -1 224 468 622 356
9 617 714 13 63 1 0 1 0 687 35 702 -22
csvwrite('file.xlsx', data1, 0, 0);
csvwrite('file.xlsx', data2, 0, 1);
My first problem is data1 prints to the spreadsheet as an array of chars (example: '#','A','r','e','... each in their own cells). How do I get it to print as the strings I am passing?
My second problem is when I csvwrite data2, data1's info is erased or overwritten. How can I write both to the same file?
Hidden away in the Tips section of the csvwrite documentation:
csvwrite does not accept cell arrays for the input matrix M. To export a cell array that contains only numeric data, use cell2mat to convert the cell array to a numeric matrix before calling csvwrite. To export cell arrays with mixed alphabetic and numeric data, where each cell contains a single element, you can create an Excel® spreadsheet (if your system has Excel installed) using xlswrite. For all other cases, you must use low-level export functions to write your data. For more information, see Export Cell Array to Text File in the MATLAB® Data Import and Export documentation.
I'd say, use xlswrite.
If you can't use xlswrite, it looks like you are stuck doing it manually as described on the page, Export Cell Array to Text File. Something along the lines of:
% write headers
fid = fopen('test.csv','w');
fprintf(fid,'%s,',data1{:});
fprintf(fid,'\n');
% write data...
fprintf(fid,[repmat('%d,',1,numel(data1)) '\n'],data2);
fclose(fid)