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

2 次查看(过去 30 天)
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?

采纳的回答

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 个)

dpb
dpb 2018-8-27
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

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by