简体   繁体   中英

Call a function of a file .m in matlab

I have a file .m and I want call a function of file .m. For example, I have the file MdeD.m:

function ESC = EDLECE(HOJA,POSF,POSC)

fid = fopen(HOJA,'r','n','UTF-8');
for i=1:POSF
    tline = fgetl(fid);
end
COL = '%s';
for i=2:POSC-1
    COL = strcat(COL, ' %s');
end
ESC = textscan(tline,strcat(COL,' %d'));
ESC = ESC{1,POSC};
end

and I want call the function EDLECE in another file .m. How do I do it?

(I sorry for my english)

Save the function in a file with the same name as the function and the .m file extension. In this case, it would be EDLECE.m . Put the file in the current working directory or in any directory in your search path.

From the other .m file, just call the function by it's name, like this:

ESC = EDLECE(HOJA,POSF,POSC);

Click here for instructions on how to view/modify your search path.

It depends whether the file MdeD.m contains only the function EDLECE or whether EDLECE.m is a subfunction in MdeD.m .

If MdeD.m only contains the EDLECE function, then you should really name the file EDLECE.m and you should be able to use the function just like any Matlab function as long as EDLECE.m is in your search path.

On the other hand, if EDLECE is a subfunction in MdeD.m , then you can't use it from another function/script or the command window. You will have to pull it out of MdeD and save it as its own function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM