fopen does not work when put text code

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 Comments

jaekeun - is this just a typo
Fullrname = strcat(Roriginaltitle,lgttext);
fidri = fopen(Fullrnam);
? You create the variable Fullrname but then use Fullrnam when trying to open the file.
If the above is just a typo (and not in your original code), have you confirmed that Fullrname is the correct name of a file in the current folder?
Fullrname = strcat(Roriginaltitle,lgttext);
fidri = fopen(Fullrnam);
First line involves a variable that has an 'e' at the end. Second line involves a variable with no 'e'.
Change
fidri = fopen(Fullrname);
to
[fidri, msg] = fopen(Fullrname);
if fidri < 0
error('Could not open file "%s" because "%s"', FUllrname, msg)
end
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?
Please show the output of
ls rev_1780.*
To. Walter Roberson
could you please explain precisely? how can I show the output ?
"could you please explain precisely?"
We want to know what files are actually in that directory. Only you can help us by following Walter Roberson's request, which is to run a command with a specific input. The command is ls, the input is 'rev_1780.*'
"how can I show the output ?"
You can copy-paste that command into the command window, like this:
>> ls rev_1780.*
and then press enter. The command will run, and print some output in the command window. Walter Roberson asked you to show us that output.
In the command window, it showed
rev_1780.lgt
Could not open file "'rev_1780.lgt'" because "No such file or directory"
@jaekeun lim : I do not understand how you get a message like "Could not open file..." from running the ls command.
Type this in the command window:
>> ls *1780.*
and show us the complete printed output.
from the ls *1780.*
I got rev_1780.lgt
Could not open file "'rev_1780.lgt'" because "No such file or directory" is from the "if" function
@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.
>> ls *.*
disp(Fullrname)
for_1780.lgt rev_1780.lgt untitled.m
'rev_1780.lgt'
>>

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 17 Aug 2019
Edited: Stephen23 on 17 Aug 2019
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 Comments

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 on 17 Aug 2019
Edited: Stephen23 on 17 Aug 2019
"..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.

Sign in to comment.

Categories

Asked:

on 16 Aug 2019

Edited:

on 17 Aug 2019

Community Treasure Hunt

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

Start Hunting!