フィルターのクリア

Write both strings and numbers to a file?

6 ビュー (過去 30 日間)
Kevin Bachovchin
Kevin Bachovchin 2014 年 6 月 5 日
コメント済み: Image Analyst 2014 年 6 月 6 日
Hello,
Is it possible using fprintf or another function to write both an array of strings and numerical values to a file? I would like to write the variables names as well as their numeric values.
Thank you, Kevin

採用された回答

José-Luis
José-Luis 2014 年 6 月 5 日
Yes you can:
s = 'I am a string';
n = pi();
fprintf(1,'%s %f',s,n); %Replace 1 with an actual fid
  4 件のコメント
Kevin Bachovchin
Kevin Bachovchin 2014 年 6 月 6 日
That works, but what would I do if I didn't know the number of elements in alphabet?
Image Analyst
Image Analyst 2014 年 6 月 6 日
That won't happen. You will know the name of your variable. If your variable name itself is variable, and stored in a variable, like your variable name is myVariableName, and sometimes that will contain the string 'alphabet' and other times it might contain 'starTrek' and other times it might be 'starWars', then treat it just as an ordinary string
fprintf(fid,'%s %f %f %f %f %f', myVariableName, numeric)
so you'll see starTrek, alphabet, or starWars depending on what happened to be inside the string. But you will and must know that the variable is called myVariableName, otherwise how can you possibly tell it what to write out?

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

その他の回答 (1 件)

Star Strider
Star Strider 2014 年 6 月 5 日
If you are exchanging files with someone else who is using MATLAB, use a mat-file. They are easy to use, binary, so are smaller, and do not need formats to write or read. They also associate the variables with their values, and when the files are opened, the variables and values (string, symbolic, numeric, cell) are automatically loaded into the workspace of the function calling them.
See matfile and associated functions for details.
  1 件のコメント
Kevin Bachovchin
Kevin Bachovchin 2014 年 6 月 6 日
mat-files are very helpful. Unfortunately in this case, I need to exchange variables and values between processors using TCP/IP communication.

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

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by