フィルターのクリア

How to change the folder path in FOPEN for creating .dat file

1 回表示 (過去 30 日間)
Halsey
Halsey 2019 年 11 月 20 日
コメント済み: Halsey 2019 年 11 月 21 日
i got an example code, but i dont understand how the path works.
  1. fid= fopen(['.\result\norm\' algo '\' ActFunc_Hid '_' ActFunc_Out '\NOUT' num2str(nout) '.dat'], 'a'); %folder path
  2. fprintf(fid, '\n \n');
  3. fprintf(fid, '%s \n',algo);
  4. fprintf(fid, '%s %s \n',ActFunc_Hid,ActFunc_Out);
  5. fprintf(fid, '%s %s %s %s %s %s \n','nhid','repeat','TrainAcc','ValAcc','TestAcc','time');
assume algo: algorithm, ActFunc_Hid : hidden layer active function, ActFunc_Out : output layer active function, NOUT : neuron output, nhid is hidden neuron, etc
For 1, why does it start with a '.\' and what does those '_' and '\' in between mean?
Question: How to change the folder path?

採用された回答

Walter Roberson
Walter Roberson 2019 年 11 月 20 日
編集済み: Walter Roberson 2019 年 11 月 21 日
Replace the first line with:
folder = fullfile('result', 'norm', algo, [ActFunc_Hid '_' ActFunct_Out]);
if ~exist(folder, 'dir')
mkdir(folder);
end
filename = fullfile(folder, sprintf('NOUT%g.dat',nout));
fid = fopen(filename, 'a');
why does it start with a '.\'
In MS Windows, that means that you want to refer to something within the current folder.
and what does those '_' and '\' in between mean
The '_' is not special: it is just a literal underscore, used to separate the Hid value from the Out value so that they are easy to read and do not run together.
In MS Windows, the '\' is a directory seperator character, marking the end of a previous subdirectory name. After a '\' there can be another subdirectory name, but the last part can instead be a file name.
MacOS and Linux use '/' instead of '\', and it turns out that MS Windows can use '/' as well.
fullfile() knows how to use '\' or '/' as appropriate for the operating system being executed on.
  4 件のコメント
Walter Roberson
Walter Roberson 2019 年 11 月 21 日
Which is to say:
  • in any case where you would have a directory separator, use fullfile() instead
  • It is cleaner and more powerful to use sprintf('CONSTANT%format', value) rather than ['CONSTANT' num2str(value)]
  • When you are writing output files with directory names that are computed, then it is quite common that your target directories will not exist, so mkdir() is often recommended
Halsey
Halsey 2019 年 11 月 21 日
That's clear, Thank you for your detailed explanations.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by