Problem using a text file

1 vue (au cours des 30 derniers jours)
Fc Fc
Fc Fc le 20 Déc 2017
Commenté : Image Analyst le 21 Déc 2017

Good evening. I have a matlab program (I'm not the author but I must modify it). In the original source (See attachment) files are opened by the code

load 'dia_221_2_0_2.txt'; %spettro diamante in diffrazione 
load 'hopg_220_4_0_2.txt'; %spettro grafite in diffrazione campione 2 ex 'hopg_220_4_0_2.txt'
load 'c60_217_0_0_2.txt'; 
load 'piro_216_0_0_2.txt'; % spettro PIROVALVOLA in diffrazione 

later the data files are used in:

ck(j) =piro_216_0_0_2(j);% sample 9 ora 7 (il più grafitico) 
ckdiadiff(j) =dia_221_2_0_2(j);%% spettro del diamante in diffrazione
      ckgradiff(j) = hopg_220_4_0_2(j);%% spettro della grafite in diffrazione
      c60diff(j)= c60_217_0_0_2(j); %%% spettro c60 in diffrazione

Now I want to change the program so that it the user can write the data file name. I tried to do it, but I couldn't! I just tried for one file (for example the dia_221_2_0_2.txt file) I first defined the variable:

global diamdiff

later I wrote

    diamdiff=input('Diamond spectrum name= ');
  [fid msg]=fopen(diamdiff,'r');

instead of

load 'dia_221_2_0_2.txt'; %spettro diamante in diffrazione

and lastly I wrote

ckdiadiff(j) =diamdiff(j);%% spettro del diamante in diffrazione

instead of

ckdiadiff(j) =dia_221_2_0_2(j);%% spettro del diamante in diffrazione

Unfortunately, running the program Matlab gives this error:

Index exceeds matrix dimensions.
Error in sp2sp3diff_Titantah (line 69)
     ckdiadiff(j) =diamdiff(j);%% spettro del diamante in diffrazione

Please, can you help me?

I think the reason is that in the original program, in the line

ckdiadiff(j) =dia_221_2_0_2(j);%% spettro del diamante in diffrazione

there is just the name of the file WITHOUT the extensions! In my code

    ckdiadiff(j) =dia_221_2_0_2(j);%% spettro del diamante in diffrazione

the variable dia_221_2_0_2 maybe is read not just like the name, but like name and extension of file! Maybe I can resolve assigning the name of the file (Without extension) to a new variable and putting this variable in this line

    ckdiadiff(j) =variable(j);%% spettro del diamante in diffrazione

But what script can I use to assign the name of the file (without extension) to a variable?

Thank you

Réponses (2)

Image Analyst
Image Analyst le 21 Déc 2017
You forgot to attach 'dia_221_2_0_2.txt' so we can't run your code.
To ask the user for a filename, use this snippet:
% Get the name of the file that the user wants to save.
% Note, if you're saving an image you can use imsave() instead of uiputfile().
startingFolder = userpath % Or "pwd" or wherever you want.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)

Fc Fc
Fc Fc le 21 Déc 2017
Hi, thank you! I resolved! I had to load the data file using the load function!
input_file_1=input('Nome FILE #1= ');
diamdiff=load(input_file_1)
  1 commentaire
Image Analyst
Image Analyst le 21 Déc 2017
And why require the user to have to use File Manager to go out and find out the file name first, so he/she can then type it in? Why not use the code I provided, where the user can simply point to it with an open file dialog box? Why make it hard on your users instead of easy?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by