Hello, I have multiple database stored in a file as : DB201706.mat DB201707.mat DB201708.mat
I want to creat a function that allows me to call the database that i want and use it in my calculation :
function [Output] = testbd()
load 'C:\MYFILE\test\DB20170730.mat'
gama = A(1);
Output = gama;
end
Is there a way to set the name of the database as an Input to the function in order to avoid entering the name manually.
function [Output] = testbd( Name)
load 'C:\MYFILE\test\ Name.mat'
gama = A(1);
Output = gama;
end
Thank you for your help

4 件のコメント

Stephen23
Stephen23 2017 年 9 月 22 日
Why not just use uigetfile ?
RHedfi
RHedfi 2017 年 9 月 22 日
I will use the compiler to compile the function that I'm creating in order to use it in excel so i'm not sure if uigetfile will works properly. Can you suggest me a code ?
Thank you Stephen
Stephen23
Stephen23 2017 年 9 月 22 日
編集済み: Stephen23 2017 年 9 月 23 日
RHedfi
RHedfi 2017 年 9 月 22 日
Yes when loading the database i want to let the user choose the database that he want

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

 採用された回答

Jan
Jan 2017 年 9 月 22 日
編集済み: Jan 2017 年 9 月 22 日

0 投票

function gama = testbd( Name)
File = fullfile('C:\MYFILE\test', sprintf('%s.mat', Name));
FileData = load(File, 'A'); % Get only variable "A"
gama = FileData.A(1);
end
Or perhaps you want to call uigetfile to determine the folder and the file?

4 件のコメント

RHedfi
RHedfi 2017 年 9 月 22 日
This function seems to answer my question perfectly but when i run
File = fullfile('C:\MYFILE\test', sprintf('%s.mat',DB201706.mat));
I've got this error "Undefined variable "BD20170630" or function "BD20170630.mat".
is there a way to fix this? Thank you Jan for your time
Stephen23
Stephen23 2017 年 9 月 23 日
@RHedfi: the code you are asking about is not from Jan Simon's answer, so it is not clear why you are using it. The source of the error is that you forgot to put single quotes around the filename to make it a string (1xN char) variable.
Jan
Jan 2017 年 9 月 23 日
@RHefi: Either
File = fullfile('C:\MYFILE\test', sprintf('%s.mat', 'DB201706'));
or:
Name = 'DB201706.mat';
File = fullfile('C:\MYFILE\test', Name);
RHedfi
RHedfi 2017 年 9 月 25 日
Thank you @Stephen Cobeldick and @Jan Simon for your precious help. the code works perfectly when i added the quotes.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Characters and Strings についてさらに検索

質問済み:

2017 年 9 月 22 日

コメント済み:

2017 年 9 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by