How can I call inside a function a file *.* ?

I have a set of files with the same name and different extensions, let them be a.env, a.shd, a.arr . I have created a script which calls those files inside a function. Is it possible to set those files as variables so that i can use them whenever it is necessary? I have already used the function genvarname but only for the 'a'. I don't know what to do with the extensions. Can anyone help? Thanks in advance.

1 件のコメント

Paschalis Garouniatis
Paschalis Garouniatis 2017 年 11 月 3 日
I used the fileparts and uigetfile functions to store the filename as a variable and they worked. However when i call them inside a function i get error. For example :
%1st case
file='/home/raptor/Desktop/BELLHOP_LAB/Bellhop_results/Ray_trace/a.ray';
[filepath,name,ext]=fileparts(file);
plotray name; hold on
plotbty name; hold on
plotati name; hold off
%2nd case
shd_file=uigetfile('*.shd');
plotshd 'shd_file'; hold on
plotbty 'name'; hold on
plotati 'name'; hold off
In both cases the functions plotray, plotshd, ... do not recognize the filename stored in the variable but the variable.

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

 採用された回答

Walter Roberson
Walter Roberson 2017 年 11 月 3 日

1 投票

basename = 'a';
dinfo = dir( [basename, '.*'] );
filenames = {dinfo.name};
Now filenames is a cell array of existing file names all with the same basic name but with the different extensions.

5 件のコメント

Paschalis Garouniatis
Paschalis Garouniatis 2017 年 11 月 3 日
Thanks a lot for your response Walter. I will give it a shot.
Paschalis Garouniatis
Paschalis Garouniatis 2017 年 11 月 3 日
編集済み: Paschalis Garouniatis 2017 年 11 月 3 日
I tried your solution. However i still got the same problem. As I mentioned above let's say that the filename is a.env . And i need to plot the file with the function plotray or the function plotshd. Because the name 'a' may vary i created a script that reads the full name of the file (a.env) and in the first case i need only 'a' to appear and in the second case 'a.env' . When i say it may vary i mean that if i execute the whole program, i may give another name to the file so i need the matlab script to read the filename everytime. That's why i don't want to set the filename inside the matlab script.
Walter Roberson
Walter Roberson 2017 年 11 月 3 日
Calls such as
plotshd 'shd_file'; hold on
should be
plotshd(shd_file); hold on
Otherwise you are passing the literal string 'shd_file' to the routine instead of passing the content of shd_file
Paschalis Garouniatis
Paschalis Garouniatis 2017 年 11 月 3 日
Is it possible to convert your code for the 1st case in which i don't need the extension of the file? For the second case that i checked it worked.
Walter Roberson
Walter Roberson 2017 年 11 月 3 日
%1st case
file='/home/raptor/Desktop/BELLHOP_LAB/Bellhop_results/Ray_trace/a.ray';
[filepath,name,ext]=fileparts(file);
plotray(name); hold on
plotbty(name); hold on
plotati(name); hold off
%2nd case
shd_file=uigetfile('*.shd');
plotshd(shd_file); hold on
plotbty(name); hold on
plotati(name); hold off

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by