error in import data

6 views (last 30 days)
xin wang
xin wang on 30 Dec 2022
Commented: Stephen23 on 30 Dec 2022
When I use the code to deal with a series of ".TIFF" document, it usually shows in the format of " error in importdata", and it cannot open the document. I really want to know where the question is. I have named the data with the format of "CPline" in another file, and whether my code cannot open the "CPline" file. Thank you for my question!
clc
clear all
close all
% Assignments
zdimension = input('Number of .tif images? ');
% Subvolume is the region in which 3D data will be cut around three-phase
% contact point
subvolume = input('Dimension of subvolume? ');
subvolume = subvolume/2;
mydata = cell(1, zdimension);
% Import .tif images in MATLAB
% !!If different number of figures, change %03d!!
for k = 1:zdimension
myfilename = sprintf('CPline%03d.tif', k);
mydata{k} = importdata(myfilename);
end
  1 Comment
Stephen23
Stephen23 on 30 Dec 2022
"Put all the files in the same directory or folder where you are running the code."
Ugh, do NOT clutter up your MATLAB search path with lots of data files. Keep your data and code separate!
Every MATLAB function that imports/exports data files also accepts absolute/relative filenames, which is exactly what you should do. As Sulaymon Eshkabilov wrote, you will find FULLFILE() very useful for this.

Sign in to comment.

Accepted Answer

VBBV
VBBV on 30 Dec 2022
mydata{k} = imread(myfilename);
  3 Comments
xin wang
xin wang on 30 Dec 2022
It still cannot open my file named "CPline" and says my file didn't exist. Could you help me how to open my file in this code? Thank you very much.
The red language you cannot identify means it cannot open my file.
VBBV
VBBV on 30 Dec 2022
Edited: VBBV on 30 Dec 2022
  • Put all the files in the same directory or folder where you are running the code.
  • Check whether the filenames exist in the working folder. As you are using number range upto zdimension imread checks for all filenames upto that range.
  • I guess your first filename may be CPline1.tif instead of CPline001.tif. Because you are using format specifier of %03d sprintf command searche for filenames that have trailing 3 digits padded with zeros. For filenames upto 1 to 10, it will search for CPline001.tif in the folder as mentioned before. To avoid it, change it to %d
  • In such case rename those filenames to CPline001.tif so on ... CPline009.tif. Similarly from 10 to 20 it should be CPline010.tif and so on... CPline099.tif.

Sign in to comment.

More Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 30 Dec 2022
Here is the corrected part of your code (supposedly all of your tif images are in your current directory of matlab):
...
for k = 1:zdimension
myfilename = strcat(['CPline' num2str(k) '.tif']);
mydata{k} = imread(myfilename);
end
  1 Comment
xin wang
xin wang on 30 Dec 2022
I want to know how I write the code to put my .tiff image in the directory of matlab. I put the code and the image in a big file, and I want my code to open a series of images, but the result shows it cannot open my image, could you help me? Thank you very much !
for k = 1:zdimension
myfilename = sprintf('CPline%03d.tif', k);
mydata{k} = importdata(myfilename);

Sign in to comment.


Sulaymon Eshkabilov
Sulaymon Eshkabilov on 30 Dec 2022
(1) Copy the files and paste them into your matlab directory
Or
(2) Use fullfile() fcn, e.g.:
FILE = fullfile('C:\Users\', '*.tif'); % Directory where the files are residing
% IMAGE_DS = imageDatastore(FILE); % Optional
LIST = dir(FILE);
N = length(LIST); % N = zdimension;
% D = zeros(250, 250, N); % Optional: If your tiff images are too large. Memory allocation for the RESIZED Images to be read
for ii = 1 : N
FFName = fullfile(LIST(ii).folder, LIST(ii).name);
DATA = imread(FFName);
% DATA = imresize(DATA(:,:,1), [250, 250]); % Optional
D{ii} = DATA;
end

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!