pngwritec error in Matlab 2017b - matlab

The pngwritec function is at the core of my program, but however, I get a series of errors when I run my main file.
Error using pngwritec
PNG library failed: Write Error.
There does not seem to be any documentation available online, yet somehow, when I ran the same program a couple of days ago, there was no issue. Is this a side effect of some new Matlab update? I'm using Matlab 2017b.
Edit:
The error shows up at the first time I need to convert an image to PNG using imwrite.
if (~exist(fullfile(denoiseFolder, [nameF '_d' extF]), 'file') | overwrite)
if saveWaveletCoeffs
waveletFileName = fullfile(saveWaveletFolder, [nameF '_w.mat']);
else
waveletFileName = '';
end
imIn = imread(fileName);
imOut = waveletDenoise(imIn, sigma0, waveletFileName);
imOut = uint8(round(imOut));
fileName = fullfile(denoiseFolder, [nameF '_d' extF]);
imwrite(imOut, fileName, 'png'); %Error here
end

Related

Difficulty printing all 6 of my matlab figures to PDF (Matlab)

I've got a script I've been working on that publishes figures I create into power point, consecutively. Each figure that is created is assigned a figure handle in the previous script. However, when I try to print each figure into a pdf, it will only return a copy of the PDF with my final figure handle (FH6). How can i get this script to print each figure, FH1-FH6, into a single pdf or their own separate pdfs. Thank you for your time
Here is an example of what i have:
close all
clear
get_mlo_data
import mlreportgen.ppt.*;
slidesFile = 'mySlides_v1.pptx';
delete(slidesFile)
slides = Presentation(slidesFile);
slide1 = add(slides,'Title Slide');
replace(slide1,'Title','Research Presentation');
replace(slide1,'Subtitle','Author');
insertmyimage(fh1,slides,'CO2 Trends')
insertmyimage(fh2,slides,'Keeling Curve')
insertmyimage(fh3,slides,'Interpolated Vs Trend')
insertmyimage(fh4,slides,'DOTS')
insertmyimage(fh5,slides,'Seasonal')
insertmyimage(fh6,slides,'Keeling Spiral')
please_god = get(groot, 'Children');
print('please_god','-dpdf')
close(slides);
function insertmyimage(FigureHandle,slides,FigureTitle)
import mlreportgen.ppt.*;
tempimgname = ['WayneImage' num2str(numel(slides.Children)+1) '.png'];
saveas(FigureHandle,tempimgname);
tempplot = Picture(tempimgname);
pictureSlide = add(slides,'Title and Content');
replace(pictureSlide,'Title',FigureTitle);
replace(pictureSlide,'Content',tempplot)
end

Problems using Matlab function eps2pdf

I have a Matlab script where I produce a figure, and then create an eps file in my current directory using the command
print('myFile','-depsc'). Immediately following, I have: mypdf = eps2pdf('myFile').
I get the error message that 'Error while creating temporary eps file: ..... cannot be accessed or does not exist'.
Has anyone had a similar problem? Any suggestions what I might be doing wrong? I'm using Ubuntu and Matlab 2017a.
Here is an example code that I type into the command line. I get the error message which I stated above.
figure()
plot(linspace(1,100),linspace(1,100)) %Simple line
print('my_plot','-depsc') %Create eps file.
mypdf = eps2pdf('my_plot'); %Should produce mypdf in my current directory.
<error message prints>
This isn't a standard function. If you read the function you will see errStr that it returns for this.
function [ok,errStr] = read_epsfilecontent( epsFile )
% Reads the content of the eps file into epsFileContent
global epsFileContent
ok = 0;
errStr = [];
fh = fopen(epsFile,'r');
if fh == -1
errStr = ['File: ' epsFile ' cannot be accessed or does not exist'];
return
end
Then we figure out when fopen returns -1
fileID = fopen(filename) opens the file, filename, for binary read
access, and returns an integer file identifier equal to or greater
than 3. MATLABĀ® reserves file identifiers 0, 1, and 2 for standard
input, standard output (the screen), and standard error, respectively.
If fopen cannot open the file, then fileID is -1.
Which means please post some of your code so we can figure out why it cannot open your file.
Edit: After some work around and it wasn't necessary to download the code this is how I solved your problem. There is another implementation called eps2xxx
While running your code I received this error
Error while creating temporary eps file: *.eps - File:
C:\Users\Ryan\Documents\MATLAB*.eps cannot be accessed or does not
exist
Which lead me to the information in the documentation here.
% Create tmp file,...
[ok,errStr] = create_tmpepsfile(source,tmpFile,orientation);
if ~ok
status = ['Error while creating temporary eps file: ' epsFile ' - ' errStr];
if nargout, result = 1; end;
if nargout > 1, msg = status; else, disp(status); end;
And I read you needed GhostScript, I wasn't sure if I had this anyways. I downloaded it and gave the full pathway to GS like the following.
figure()
fullgspath = 'C:\Program Files\gs\gs9.23\bin\gswin64c.exe';
plot(linspace(1,100),linspace(1,100)); %Simple line
print('my_plot','-depsc');
eps2xxx('my_plot.eps',{'pdf'},fullgspath);
which created your nice little pdf here.

MATLAB Error using importdata

I get an error at the importdata line saying
"Error using importdata (line 137)
Unable to open file."
when I write in 'specimenAl.dat' manually into the function, the program runs fine. How can I make use of the importarray function while defining the argument as an element of an array?
material = {('specimenAl.dat'), ('specimenSt.dat')};
A = importdata(material(1));
Data = A.data;
Force = Data (:,2);
Displacement = Data (:,1);
Strain = Data (:,3);
I had the same problem. fixed it by using {} in
A = importdata(material{1});
Hope this helps!

octave large data set, doesn't disp() or write properly

I have a huge data set that I'm caching, then writing filtered analysis data to disk. I have various disp() commands in my code, along with fprintf() calls.
I'd like to see the results both in the file, and on the screen while the processes are running, but what I'm finding is that I get nothing until I terminate the program, at which point all the data is written into my file and the disp() floods the terminal.
Would there be a way to force disp() and the fprintf() to execute as they're being processed??
Here's an example:
function one(varargin)
setenv GNUTERM 'x11';
dirname = strcat(pwd, '/fileset');
files = dir(dirname);
disp('reading directory'), disp(dirname);
fileidx = find(~[files.isdir]);
out = fopen('write_data.txt', 'w');
fprintf(out, '"--- var a[0]", "--- var [1]";\n');
numfiles = length(fileidx);
for i = 1:numfiles
dispstring = sprintf('processing file %d of %d...', i, numfiles);
disp(dispstring);
filename = [dirname, '/', files(fileidx(i)).name];
disp(filename);
fid = fopen(filename, 'r');
%some processing here to obtain timevalues and maxvars
for i = 1:length(timevalues)
fprintf(out, '%d, %d;\n', timevalues(i), maxvars(i));
end
end
fclose(out);
end
I saw this post, but I wasn't sure which of the methods suggested applied to me. It also seemed like fflush() was meant for pushing data into a plot at higher priority.
I have had this problem before and you do you fflush to solve it. Write
fflush(stdout);
to force the terminal to be updated with the results of all the prints and disps to stdout that came before the call to fflush(stdout). I'm not sure if you should bother flushing the output to the file as it will probably make your code slower, but if you want to you can do
fflush(out);

How to read a lot of DICOM files with Matlab?

I am using a script which generate a collection of strings in a loop:
'folder1/im1'
'folder1/im2'
...
'folder1/im3'
I assign the string to a variable, when I try to execute the img = dicomread(file); function I get the following error:
Error using dicomread>newDicomread (line 164)
The first input argument must be a filename or DICOM info struct.
Error in dicomread (line 80)
[X, map, alpha, overlays] = newDicomread(msgname, frames);
Error in time (line 14)
img = dicomread(file);
However, using the command line I don't get errors: img = dicomread('folder1/im1').
The code is the next:
for i=1:6 %six cases
nameDir = strcat('folder', int2str(i));
dirData = dir(nameDir);
dirIndex = [dirData.isdir];
fileList = {dirData(~dirIndex).name}; % list of files for each directory
n = size(fileList);
cd(nameDir);
for x = 1:n(2)
img = dicomread(strcat(pwd(), '/', fileList(x)));
end
cd('..');
end
What could be the error?
You've figured it out by now, haven't you.
Based on what you've written, you test
img = dicomread('folder1/im1');
when what you are having trouble with is
img = dicomread(file);
You need to actually test the line you are having trouble with. I would recommend:
putting a break point in test.m a the line img = dicomread(file). When you get to that line you can see what file is equal to. Also do whos file to make sure it is of class char and not a cell array or something random.
If you still want help, edit your original post and show the code where you assign those strings to file and tell us what happens when you type img = dicomread(file) at the command prompt.