error in import data
2 views (last 30 days)
Show older comments
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
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.
Accepted Answer
VBBV
on 30 Dec 2022
mydata{k} = imread(myfilename);
3 Comments
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.
More Answers (2)
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
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
0 Comments
See Also
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!