필터 지우기
필터 지우기

Pass text file as argument to function?

조회 수: 3 (최근 30일)
Janell Lopez
Janell Lopez 2016년 9월 25일
댓글: Janell Lopez 2016년 9월 26일
Hi,
I have a homework assignment where we have a main function that calls 3 other functions: one reads a system of equations from a text file, one solves the system, and one writes the system and its solutions to a new text file. I'm supposed to pass the name of the text file as the argument to the main function (or have the user be able to do so) so that I don't have to modify the code at all to be able to read from different text files. Right now I have a script instead of a function for the main function, and everything works if you modify the name of the text file in the code. How do I change it so that I can call the function with the text file as the argument? Do you pass it as a string? How do I call the subfunctions within the main function with a text file name as an argument?
Thank you!

채택된 답변

Image Analyst
Image Analyst 2016년 9월 25일
Let's say your main function is called main.m and it's a script right now. Simply put this line at the first line of that main.m file:
function main(fullFileName)
then inside the function call fgetl() or whatever you want. But then to pass the string into main, you're going to have to call uigetfile() either from the command line, or from another script or function.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by