How do I make a user defined function with the input being the file name in a string?

I want to make a user defined function where the user can just type in the file name as the input. For example, if my file is called 'graphs.csv' and my function is called power, I want to be able to do
power('graphs.csv') and for the function to run. How do I make this as my input?

댓글 수: 2

Do NOT use the name power, as this is the name of the very important inbuilt power operation!
dpb
dpb 2018년 8월 27일
편집: dpb 2018년 8월 27일
Good catch, Stephen; I whiffed on that important detail...

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2018년 8월 27일
편집: Stephen23 2018년 8월 27일
Sure, this is easy (do NOT use the name power, as this is already used by MATLAB):
function myfun(filename)
data = csvread(filename);
...
end
and call it like this:
myfun('graphs.csv')
How to define functions is clearly explained in the function help.

추가 답변 (1개)

The users will probably very quickly get very tired of having to type in a fully-qualified file name.
I'd suggest something more on the lines of
function [returns?]=power
fn=uigetfile;
if fn==0, return, end
% function does whatever with the returned filename following
...
end

카테고리

도움말 센터File Exchange에서 Standard File Formats에 대해 자세히 알아보기

질문:

2018년 8월 26일

편집:

dpb
2018년 8월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by