fprintf function writes irrelevant data into my file

2 次查看(过去 30 天)
I wants to write meas matrix of Iris dataset into a txt file in three iterations, with each iteration writing 50 rows or data points. Below is the code. After executing it my output file contains irrelevant data. I inspected the temp variable. But it contains properly the last 50 data points. What went wrong here?
range = [1,50;51,100;101,150];
data = meas;
fid = fopen('iris_data.txt', 'a');
for i = 1:3
tmp = data(range(i,1):range(i,2),:);
fprintf(fid, '%f %f %f %f\n', tmp);
end
fclose(fid);
Pls clarify!

采纳的回答

Voss
Voss 2024-2-26
编辑:Voss 2024-2-26
fprintf operates column-wise when given an array as input. So for matrix tmp, fprintf prints tmp(1,1) then tmp(2,1), ..., tmp(end,1), (i.e., down the first column of tmp first), then it starts on the second column and prints tmp(1,2), tmp(2,2), ..., etc., until the last element of tmp.
To print row-wise (across the rows of tmp first), simply pass the transpose of tmp to fprintf.
Example:
range = [1,50;51,100;101,150];
% data = meas;
data = rand(150,4)
data = 150×4
0.7966 0.5737 0.9403 0.3655 0.3381 0.6147 0.4533 0.4342 0.8237 0.4744 0.7845 0.2163 0.6231 0.8623 0.9023 0.7879 0.9514 0.2122 0.6027 0.5193 0.7194 0.8488 0.3262 0.9797 0.0199 0.6542 0.5303 0.9446 0.4297 0.5473 0.4289 0.5397 0.7277 0.1972 0.0218 0.2461 0.8094 0.6200 0.3109 0.7519
fid = fopen('iris_data.txt', 'a');
for i = 1:3
tmp = data(range(i,1):range(i,2),:);
fprintf(fid, '%f %f %f %f\n', tmp.'); % <- tranposed
end
fclose(fid);
% check the file's contents:
type('iris_data.txt')
0.796608 0.573716 0.940330 0.365470 0.338104 0.614670 0.453345 0.434154 0.823711 0.474408 0.784532 0.216260 0.623112 0.862271 0.902341 0.787915 0.951388 0.212159 0.602732 0.519335 0.719418 0.848844 0.326197 0.979698 0.019945 0.654194 0.530255 0.944562 0.429711 0.547274 0.428896 0.539668 0.727748 0.197227 0.021791 0.246064 0.809448 0.620045 0.310947 0.751939 0.944291 0.703707 0.983130 0.855440 0.270780 0.251450 0.576403 0.617887 0.728384 0.090845 0.098319 0.241729 0.916850 0.320136 0.160276 0.484155 0.486117 0.574011 0.926226 0.607138 0.206061 0.656239 0.384690 0.010004 0.041487 0.511585 0.002853 0.405765 0.490687 0.308983 0.105843 0.803030 0.896096 0.637228 0.559850 0.238240 0.524963 0.870428 0.674399 0.662380 0.531157 0.859966 0.752705 0.268797 0.139707 0.399779 0.330070 0.481638 0.401618 0.209917 0.783200 0.978098 0.573851 0.331611 0.633014 0.408532 0.498484 0.072790 0.547687 0.728540 0.301108 0.401290 0.785295 0.307191 0.473774 0.639917 0.282754 0.183203 0.157391 0.966926 0.113891 0.851202 0.576856 0.058167 0.125859 0.263212 0.283811 0.173881 0.024041 0.338907 0.140356 0.926013 0.396282 0.718834 0.145473 0.302285 0.698757 0.378406 0.409757 0.646770 0.994155 0.357296 0.571739 0.358293 0.206640 0.268166 0.679702 0.910760 0.765380 0.912009 0.592445 0.342932 0.069698 0.328217 0.362431 0.705796 0.734776 0.321414 0.437603 0.119327 0.471346 0.241670 0.790261 0.640711 0.708852 0.905825 0.402320 0.145556 0.235735 0.822787 0.136766 0.329964 0.400589 0.403540 0.084362 0.736580 0.170749 0.576361 0.021379 0.601187 0.049998 0.134408 0.425733 0.350466 0.055853 0.751774 0.564624 0.686029 0.526219 0.340500 0.907418 0.957768 0.759846 0.846776 0.124290 0.192081 0.456192 0.874086 0.101983 0.251587 0.177020 0.482086 0.366005 0.719736 0.884477 0.869868 0.556679 0.401577 0.586374 0.841304 0.123013 0.224334 0.945291 0.618545 0.412261 0.448144 0.021929 0.776566 0.985201 0.029288 0.061865 0.455467 0.857886 0.239486 0.029346 0.905974 0.702086 0.879903 0.521539 0.491594 0.131677 0.973508 0.043939 0.821011 0.730223 0.490193 0.909894 0.598067 0.638320 0.822729 0.249040 0.816405 0.455368 0.182280 0.082225 0.133857 0.034873 0.301527 0.378305 0.158103 0.564750 0.314699 0.306609 0.412623 0.309590 0.918620 0.449294 0.130875 0.859741 0.820411 0.060647 0.006908 0.602833 0.804656 0.622946 0.887856 0.789280 0.713172 0.409861 0.238551 0.833390 0.345656 0.463614 0.562406 0.797881 0.847440 0.051989 0.136877 0.011070 0.656322 0.457275 0.047316 0.030175 0.204585 0.257313 0.949645 0.211697 0.574699 0.258309 0.030537 0.705389 0.848281 0.565714 0.223707 0.591074 0.478062 0.247536 0.863224 0.671512 0.586856 0.159211 0.201922 0.160713 0.368294 0.324228 0.455373 0.315735 0.117384 0.691708 0.586884 0.060369 0.647993 0.769100 0.227493 0.408869 0.291359 0.880992 0.407734 0.113398 0.644520 0.999623 0.899618 0.946155 0.480697 0.348722 0.672063 0.491806 0.611926 0.363012 0.698214 0.284015 0.989688 0.386993 0.702259 0.650906 0.734116 0.788313 0.004655 0.687626 0.585118 0.658104 0.162832 0.970357 0.057048 0.885198 0.128998 0.531112 0.211469 0.204251 0.856810 0.016828 0.791116 0.593669 0.256362 0.183988 0.725037 0.900499 0.018882 0.523975 0.463391 0.794438 0.018412 0.289736 0.438561 0.976803 0.729039 0.212909 0.960805 0.792400 0.391537 0.777000 0.138231 0.973532 0.500144 0.322849 0.924484 0.482038 0.534699 0.227318 0.898244 0.456992 0.995932 0.974017 0.145341 0.849449 0.300498 0.735993 0.868377 0.442441 0.541764 0.094846 0.788306 0.487865 0.520415 0.323927 0.937206 0.858579 0.717947 0.895458 0.613860 0.141741 0.408906 0.963969 0.554738 0.648748 0.970064 0.636944 0.302853 0.235455 0.629859 0.958735 0.076706 0.167498 0.147297 0.345622 0.412289 0.932157 0.679224 0.743126 0.124293 0.473726 0.217875 0.116781 0.089160 0.621388 0.415791 0.208932 0.707999 0.384320 0.564658 0.676485 0.421048 0.900422 0.895682 0.339849 0.180972 0.050639 0.708099 0.109258 0.704573 0.569295 0.058734 0.339495 0.883416 0.968261 0.684073 0.136482 0.125238 0.038196 0.713792 0.846577 0.972549 0.218706 0.922546 0.803252 0.121695 0.056663 0.973559 0.739073 0.149323 0.514568 0.907587 0.999227 0.623894 0.010685 0.427023 0.320041 0.641142 0.028962 0.649783 0.368586 0.479924 0.949873 0.121117 0.017730 0.112999 0.533435 0.510254 0.231674 0.835553 0.194522 0.535738 0.439716 0.119626 0.283140 0.836066 0.896409 0.419600 0.496166 0.202148 0.299865 0.899659 0.106674 0.874136 0.033319 0.048401 0.416774 0.336375 0.950314 0.179859 0.082519 0.375445 0.556576 0.981310 0.569112 0.528779 0.929488 0.063730 0.400012 0.541144 0.576061 0.453414 0.135129 0.131422 0.725898 0.331034 0.534922 0.786177 0.567849 0.211221 0.547770 0.812435 0.893250 0.010429 0.558922 0.085828 0.769174 0.923923 0.860045 0.331264 0.390422 0.385409 0.914047 0.583558 0.576442 0.404121 0.808610 0.952730 0.511620 0.749383 0.380758 0.798636 0.511679 0.778885 0.499868 0.611774 0.479751 0.992584 0.389100 0.603551 0.258845 0.077586 0.945070 0.505772 0.495853 0.000470 0.272381 0.279269 0.748630 0.874843 0.181476 0.153816 0.532622 0.637021 0.891422 0.064919 0.932057 0.286399 0.175866 0.650066 0.160170 0.707791 0.089662 0.816265 0.818529 0.634330 0.233060 0.621709 0.197126 0.516635 0.149465 0.837872 0.660686 0.673902 0.753458 0.319370 0.570806 0.416311 0.706405 0.637598 0.633351 0.137599 0.860110 0.246850 0.915382 0.638592 0.780752 0.024237 0.058129 0.366230 0.761552 0.774763 0.761576 0.073398 0.082230 0.096986 0.302191 0.972751 0.015593 0.956528
You can see that the contents of the file match the data matrix.
Another thing that may be causing confusion is that you are opening the file in append mode (fopen(_,'a')), in which case the data you fprintf is appended to the end of the file. If that's not what's intended, you may be looking at the beginning of the file and seeing not the values of tmp but instead some irrelevant data that was written previously. If you want to write the file "from scratch" use write mode (fopen(_,'w')).

更多回答(1 个)

Steven Lord
Steven Lord 2024-2-26
We cannot run your code because we don't have the meas matrix. Please post the code that creates it or attach a MAT-file with at least the first 150 rows of that matrix to this post.
Also show a sample of the iris_data.txt file that shows the "irrelevant data". Does it contain the correct data just not in the order you expected? Note that the documentation page for the fprintf function states in part "fprintf(fileID,formatSpec,A1,...,An) applies the formatSpec to all elements of arrays A1,...An in column order, and writes the data to a text file." (emphasis added.) So perhaps you just need to transpose tmp before writing it to the file.
Compare the following two fprintf calls, and note that the second uses M.' instead of just M.
M = magic(3)
M = 3×3
8 1 6 3 5 7 4 9 2
fprintf('%d %d %d\n', M)
8 3 4 1 5 9 6 7 2
fprintf('%d %d %d\n', M.')
8 1 6 3 5 7 4 9 2

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by