fopen does not work when put text code

5 ビュー (過去 30 日間)
jaekeun lim
jaekeun lim 2019 年 8 月 16 日
編集済み: Stephen23 2019 年 8 月 17 日
fidfi =fopen('for_1780.lgt');
textDatafi = textscan(fidfi, '%s%s%s%s','delimiter', ' ');
fclose(fidfi);
gtitle = [textDatafi{1,2}{7,1}]; %extraction of file name
splitgtitle = extractAfter(gtitle,12);
lgttext = (".lgt'");
Roriginaltitle = insertBefore(splitgtitle,1,"'rev_"); %for reverse file name
Fullrname = strcat(Roriginaltitle,lgttext);
fidri = fopen(Fullrname);
textDatari = textscan(fidri, '%s%s%s%s','delimiter', ' ');
fclose(fidri);
I thought after making the charactor "'rev_1780.lgt'", if the charactor is put in the "fidri = fopen(Fullrname);", it will run well. However, the code gave me integer -1.
What's the problem? if I see the matrix, they show "'rev_1780.lgt'" but it did not work.... the 'rev_1789' file is located in the same folder with 'for_1780.lgt'
sorry I copyed wrong way. I changed code again in the original file there is no typo.
So if I copy and paste of the Fullrname's data at the fopen, really it woked well. However, when I put Fullrname directly, it did not work.
Additionally,
[fidri, msg] = fopen(Fullrname);
if fidri < 0
error('Could not open file "%s" because "%s"', Fullrname, msg)
end
Although changed to this code instead of fidri = fopen(Fullrname);, just they showed error msg.
Could not open file "'rev_1780.lgt'" because "No such file or directory"
I work at the folder where the file is. should I do code more something?
To. Walter Roberson
could you please explain precisely? how can I show the output ?
Additionally, this is my coding now. Unfortunately, could not upload lgt file here
clear
clc
%% for obtaing information of data (forward)
fidfi =fopen('for_1780.lgt');
textDatafi = textscan(fidfi, '%s%s%s%s','delimiter', ' ');
fclose(fidfi);
gtitle = [textDatafi{1,2}{7,1}]; %extraction of file name
splitgtitle = extractAfter(gtitle,12);
lgttext = (".lgt'");
Roriginaltitle = insertBefore(splitgtitle,1,"'rev_"); %for reverse file name
Fullrname = strcat(Roriginaltitle,lgttext);
ls rev_1780.*
[fidri, msg] = fopen(Fullrname);
if fidri < 0
error('Could not open file "%s" because "%s"', Fullrname, msg)
end
textDatari = textscan(fidri, '%s%s%s%s','delimiter', ' ');
fclose(fidri);
In the command window, it showed
rev_1780.lgt
Could not open file "'rev_1780.lgt'" because "No such file or directory"
  12 件のコメント
Stephen23
Stephen23 2019 年 8 月 17 日
編集済み: Stephen23 2019 年 8 月 17 日
@jaekeun lim : Please show the output of these two commands:
>> ls *.*
>> disp(Fullrname)
Please just copy and paste the entire section from the command window, including the commands themselves. Do not edit or change it.
jaekeun lim
jaekeun lim 2019 年 8 月 17 日
>> ls *.*
disp(Fullrname)
for_1780.lgt rev_1780.lgt untitled.m
'rev_1780.lgt'
>>

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

回答 (1 件)

Stephen23
Stephen23 2019 年 8 月 17 日
編集済み: Stephen23 2019 年 8 月 17 日
You are adding extra single quotes, here:
lgttext = (".lgt'");
% ^ superfluous
Roriginaltitle = insertBefore(splitgtitle,1,"'rev_");
% ^ superfluous
These single quotes are causing the problem. Get rid of them.
Either use character arrays:
lgttext = ('.lgt');
Roriginaltitle = insertBefore(splitgtitle,1,'rev_');
or string arrays, whichever you prefer. But there is no point in adding extra single quotes to those strings, which serve no obvious purpose.
Personally I find that filename conversion overly complex, and would just do something like this:
>> inp = 'for_1780.lgt'
inp =
for_1780.lgt
>> out = strrep(inp,'for_','rev_')
out =
rev_1780.lgt
  2 件のコメント
jaekeun lim
jaekeun lim 2019 年 8 月 17 日
Really Thank you so much !
After removing single quotes, it works well. By the way, I wonder one thing that normally when fopen function is used, single quotes are used between file name. However, in this case, without single quotes, file name is put.
Why is it different?
Stephen23
Stephen23 2019 年 8 月 17 日
編集済み: Stephen23 2019 年 8 月 17 日
"..normally when fopen function is used, single quotes are used between file name..."
Not at all:
  • Single-quotes are an operator that creates a character vector.
  • Double-quotes are an operator that creates a scalar string.
Do not confuse those operators with actually having those characters inside the character vector or string itself: they are NOT part of the character vector/string!
The function fopen requires its first argument to be a character vector or a scalar string, but it really knows nothing at all about how strings/character vectors are defined.
"However, in this case, without single quotes, file name is put. "
I have no idea what that means. But you seem to be confusing the operators that create character vectors and strings (which, just like in many other languages, are denoted in the code by ' and "), with those characters actually occuring inside the character vector or string.
"Why is it different?"
What is different?
MATLAB is quite consistent:
  • "abc" defines a scalar string containing the three characters abc.
  • 'abc' defines a 1x3 character vector containing the characters abc.
Neither the string nor the character vector contains the characters " or '.
Remember to accept my answer if it helped you! accepting answers is an easy way to show your appreciation to the volunteers who help you on this forum.

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

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by