필터 지우기
필터 지우기

Assigning file name as variable name

조회 수: 9 (최근 30일)
Burak Dermenci
Burak Dermenci 2015년 7월 7일
댓글: Image Analyst 2015년 7월 7일
Hi everyone,
I would like to write a program. The program must ask users to enter the name of the text we're importing and change the variable name each time. The code I wrote is below:
name = input('Enter the name of a file: ','s');
s=importdata('name.txt');
data=s.data
t_name=data(:,1);
mass_name=data(:,4);
dtg_name=data(:,7);
[ax,p1,p2]=plotyy(t_name,mass_name, t_name,dtg_name,'plot','plot')
I have mainly 2 problems. Importing file from the code generated by input command and assigning t_name, dtg_name etc. variables each time. How can I fix it?
Thank you for your help

채택된 답변

Image Analyst
Image Analyst 2015년 7월 7일
Try this:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
s=importdata(fullFileName);
  댓글 수: 2
Burak Dermenci
Burak Dermenci 2015년 7월 7일
This is not what I asked but exactly what I want.
Thank you for your reply
Image Analyst
Image Analyst 2015년 7월 7일
If the importdata() is not working with your data, then you can try another input option like csvread(), textscan(), or whatever, or else write your own custom reading function.

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

추가 답변 (1개)

Titus Edelhofer
Titus Edelhofer 2015년 7월 7일
Hi,
you should use name as a variablename to importdata:
name = input('Enter the name of a file: ','s');
s=importdata(name);
Titus
  댓글 수: 1
Burak Dermenci
Burak Dermenci 2015년 7월 7일
I tried this and it gives error:
Unable to load file.
Use TEXTSCAN or FREAD for more complex formats.
Caused by:
Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.

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

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by