現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Writing variables in .csv file and saving it
7 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have a functionality that works perfectly fine. But the output has a set of Numeric value and text strings.
for ex: abc, 1, def,1
def,2, rfg, 1
How can i save the output in a .csv file? do i need to put all my output values of the code to a Matrix then use csvwrite() function?
Please suggest your inputs
回答 (1 件)
Ameer Hamza
2020 年 6 月 6 日
If you have R2019a or later, you can save the data in a cell array and use writecell()
C = {'abc', 1, 'def' 1; 'def', 2, 'rfg', 1};
writecell(C, 'test.txt')
25 件のコメント
Ganesh Kini
2020 年 6 月 6 日
I have 15,000 entries ( as the output for the code) in that case how do i write it down ?
Ganesh Kini
2020 年 6 月 6 日
Actually i had posted a query earlier, but didnt get any suggestion.
Could you please check this link Example
It would be of great help
Ameer Hamza
2020 年 6 月 6 日
It is not clear in which format are you getting the output. Is it just printed on the command window?
Ganesh Kini
2020 年 6 月 6 日
Yes, I have specified in fprintf() in the octave code and then i will get that output in the command window.
Please let me know if you need more clarifications
Ameer Hamza
2020 年 6 月 6 日
I think instead of printing to the command window, you can print directly to the CSV file. The actual will depend on how the values are saved inside the for-loop.
Ganesh Kini
2020 年 6 月 6 日
編集済み: Ganesh Kini
2020 年 6 月 6 日
Okay, could do you please suggest what can be done to this?
Do you want the printf statement that i have used?
I am not able to understand how to write it
Ameer Hamza
2020 年 6 月 6 日
The current printf statements might not be useful. The actual issue is how all the values are saved? Are they saved in some variables?
Ganesh Kini
2020 年 6 月 6 日
yes they are saved in different variables. each value is computed in the for-loop and then we have the value assigned to each of the variable ( both string and floating values ) and then i write everything in fprint() statement
Ameer Hamza
2020 年 6 月 6 日
If you have all the values if a same filed inside one variable then you may try fprintf like this
fprintf('Place: ')
fprintf('%s,', place_values)
Ganesh Kini
2020 年 6 月 6 日
編集済み: Ganesh Kini
2020 年 6 月 6 日
Can you please let me know the full flow of the code? so that it would be easy to me to understand after the above what needs to be done and how do we write in that particular format
Is it possible to reply in this link so that it would be easy for others to understand the flow and how the code needs to be written
Ameer Hamza
2020 年 6 月 6 日
I meant that you can do it for each line. For example,
fprintf('Place: ')
fprintf('%f,', place_values)
fprintf('\n');
fprintf('Repeated Place: t,')
fprintf('f,', ones(1,size(place_values)))
fprintf('\n');
fprintf('Temperature: ')
fprintf('%f,', temperature_values)
fprintf('\n');
..
..
% similarly for other fields.
Ganesh Kini
2020 年 6 月 6 日
Okay i will try and how do i save it in .csv file ?
Please suggest me the code for that
Ameer Hamza
2020 年 6 月 6 日
fprintf() can write to a file as well
fid = fopen('filename.csv');
fprintf(fid, 'Place: ')
fprintf(fid, '%f,', place_values)
fprintf(fid, '\n');
fprintf(fid, 'Repeated Place: t,')
fprintf(fid, 'f,', ones(1,size(place_values)))
fprintf(fid, '\n');
fprintf(fid, 'Temperature: ')
fprintf(fid, '%f,', temperature_values)
fprintf(fid, '\n');
..
..
% similarly for other fields.
fclose(fid)
Ganesh Kini
2020 年 6 月 6 日
Okay i will try.
Is it possible to write using csvwrite function?
i wanted to know about that
Ganesh Kini
2020 年 6 月 7 日
Hi Ameer,
When i try to run this code
[fid1, msg1] = fopen('file.csv');
fprintf(fid1, 'temperature ');
fprintf(fid, '%3.0f,', temp );
fprintf(fid, '\n');
fclose(fid);
Error
>> msg1
msg1 = No such file or directory
>> fid1
fid1 = -1
I should create a csv file and then enter the values. How can we do it ?
Ameer Hamza
2020 年 6 月 8 日
You need to specify that you want to open the file for writing
[fid1, msg1] = fopen('file.csv', 'w');
Ganesh Kini
2020 年 6 月 9 日
Hi I was running this code
[fid, msg] = fopen ( 'file1.csv' , 'wt' );
assert (fid> = 3, msg)
fprintf (fid, '% s \ t% s \ t% s \ n' , 'Temperature' , 'Value_n' , 'Value_p' );
for
for
for
...
fprintf (fid, '% 3.0f \ t% 1.1f \ t% 1.1f \ n' , temp, vnw, vpw);
end
end
end
fclose (fid);
Output of the code
Temperature Value_n Value_p ,-40 0.2 0.1 20 0.1 0.4 120 0.5 4.5
where as i should get
Temperature, -40 20 120
Value_n, 0.2 0.1 0.5
Value_p, 0.1 0.4 4.5
How can i change to get the above output?
Ameer Hamza
2020 年 6 月 9 日
Try to write it seperately
[fid, msg] = fopen ( 'file1.csv' , 'wt' );
assert (fid> = 3, msg)
fprintf (fid, '% s \t' , 'Temperature');
for
for
for
...
fprintf (fid, '% 3.0f \t' , temp);
end
end
end
fprintf (fid, '\n' , temp);
for
for
for
...
fprintf (fid, '% 3.0f \t' , vnw);
end
end
end
fprintf (fid, '\n' , temp);
% same for vpw
fclose (fid);
Ganesh Kini
2020 年 6 月 9 日
Yes that would work but i can not use for loop like that, because i have 10 variables
10*3 -- 30 for loops will not be feasible.
Is there any alternate way?
Ameer Hamza
2020 年 6 月 9 日
Something like this
[fid, msg] = fopen ( 'file1.csv' , 'wt' );
assert (fid> = 3, msg)
labels = {'Temperature', 'Value_n', 'Value_p'};
vars = {temp, vnw, vpw}
for i=1:numel(labels)
fprintf (fid, '% s \t' , labels{i});
for
for
for
...
fprintf (fid, '% 3.0f \t' , vars{i});
end
end
end
fprintf (fid, '\n' , temp);
end
fclose (fid);
Ganesh Kini
2020 年 6 月 9 日
編集済み: Ganesh Kini
2020 年 6 月 9 日
Hi Ameer,
Tried your method, it didn't work as expected
Temperature t 60 60 60 60 60 60 60 60 60 60 60 60
Value_n t 1 1 1 1 1 1 1 1 1 1 1 1
Value_p t -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2
Could you please tell me where i can change this?
The value needs to change after one complete execution, but it is not doing that
Ganesh Kini
2020 年 6 月 9 日
size(backend_full)
Output
ans =
12 7
But when i write the above code for repeated values
fprintf (fid2, '\nEnable,t,');
fprintf (fid2, 'f,', ones(1,size(backend_full)));
Output on the csv file
Enable,t,f,
Please let me know
Ameer Hamza
2020 年 6 月 10 日
編集済み: Ameer Hamza
2020 年 6 月 10 日
Are all the variables calculated before this line?
vars = {temp, vnw, vpw}
or are they calculated inside for-loop?
参考
カテゴリ
Help Center および File Exchange で Data Type Identification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)