When this piece of code is executed the program uses the matlabcontrol library to open MATLAB and execute the given lines inside the eval(). The problem is in the first line i.e. m.eval("image1=imread('D:/My Hand Gestures/f.jpg');"); takes a fixed String value as input. But here i want to store the path in a variable and pass it to the imread() function. How am i supposed to do that? Any help is appreciated. Here is the code.
package Interface;
import matlabcontrol.MatlabConnectionException;
import matlabcontrol.MatlabInvocationException;
import matlabcontrol.MatlabProxy;
import matlabcontrol.MatlabProxyFactory;
/**
*
* #author Rajdeep
*/
public class Output {
private static String check;
private static String path;
Output(){
//default constructor
}
Output(String s,String p){
check = s;
path=p;
}
//GrayScale Conversion Function
public static void grayScale(String p,MatlabProxy m)throws MatlabConnectionException, MatlabInvocationException{
m.eval("image1=imread('D:/My Hand Gestures/f.jpg');");
m.feval("image1","imread(path)");
m.eval("figure,imshow(image1);");
m.eval("image_gray=rgb2gray(image1);");
m.eval("figure,imshow(image_gray);");
m.eval("final_image=imresize(image_gray,0.03125);");
m.eval("figure,imshow(final_image);");
m.eval("imwrite(final_image,'C:/Users/Desktop/f.jpg');");
//Disconnect the proxy from MATLAB
m.disconnect();
}
//Median Filtering Function
public static void Filter(String p, MatlabProxy m)throws MatlabConnectionException, MatlabInvocationException{
m.eval("I=imread('D:/gestures/f.jpg');");
m.eval("J = medfilt2(I, [4 4]);");
m.eval("figure,imshow(J);");
m.eval("imwrite(J,'C:/Users/Rajdeep/Desktop/f.jpg');");
//Disconnect the proxy from MATLAB
m.disconnect();
}
/**
*
* #param args
* #throws MatlabConnectionException
* #throws MatlabInvocationException
*/
public static void main(String[] args) throws MatlabConnectionException, MatlabInvocationException
{
//Create a proxy, which we will use to control MATLAB
MatlabProxyFactory factory = new MatlabProxyFactory();
MatlabProxy proxy = factory.getProxy();
Output out=new Output("GrayScale","D:/My Hand Gestures/f.jpg");
if(check == "GrayScale") {
grayScale(path, proxy);
}
if(check== "Filter"){
Filter(path,proxy);
}
}
}
Here i created a path variable that has a predefined path. I want to use this variable instead of giving the path as in the above mentioned process.
I would try this to start with. See if it helps.
String path_variable = "D:/My Hand Gestures/f.jpg";
m.eval("image1=imread('" + path_variable + "');");
Related
public Life(List<class> classes, int schoolwork) {
super(classes, schoolwork);
}
I am trying to make a Life object out of this code but am not doing it right what I have is
Life life = new Life();
but I can't figure out how to get the parameters right to include classes and schoolwork.
I tried to interpret your problem and I wrote a piece of code where I show you how to create a Life object by passing it the correct parameters.
Maybe your problem was creating the List object (?)
CODE
public class Main {
public static void main(String[] args) throws IOException {
List<CustomClass> customClass = new ArrayList<>();
int schoolwork = 1;
Life life = new Life(customClass, schoolwork);
}
}
class CustomClass {
}
class Life {
/**
* Constructor
*
* #param customClass
* #param schoolwork
*/
public Life(List<CustomClass> customClass, int schoolwork) {
// super(strings, schoolwork);
}
}
One.java
public class One {
String asd;
public class() {
asd="2d6"
}
public static void main(String args[]) {
Two a = new Two();
}
}
Two.java
public class Two {
ArrayList<String>data;
String asd;
public Two(String asd){
this.asd=asd;
data.add(this.asd);
}
}
How do I use this asd value of second for third class calling from first class's main method.
**Third class**
Per comments of #Maroun Maroun and #Bennyz, you can create a getter and setter method in your Two class:
import java.util.ArrayList;
public class Two {
ArrayList<String> data;
String asd;
public Two(String asd) {
this.asd = asd;
data = new ArrayList<>(); //<-- You needed to initialize the arraylist.
data.add(this.asd);
}
// Get value of 'asd',
public String getAsd() {
return asd;
}
// Set value of 'asd' to the argument given.
public void setAsd(String asd) {
this.asd = asd;
}
}
A great site to learn about this while coding (so not only reading), is CodeAcademy.
To use it in a third class, you can do this:
public class Third {
public static void main(String[] args) {
Two two = new Two("test");
String asd = two.getAsd(); //This hold now "test".
System.out.println("Value of asd: " + asd);
two.setAsd("something else"); //Set asd to "something else".
System.out.println(two.getAsd()); //Hey, it changed!
}
}
There are also some things not right about your code:
public class One {
String asd;
/**
* The name 'class' cannot be used for a method name, it is a reserved
* keyword.
* Also, this method is missing a return value.
* Last, you forgot a ";" after asd="2d6". */
public class() {
asd="2d6"
}
/** This is better. Best would be to create a setter method for this, or
* initialize 'asd' in your constructor. */
public void initializeAsd(){
asd = "2d6";
}
public static void main(String args[]) {
/**
* You haven't made a constructor without arguments.
* Either you make this in you Two class or use arguments in your call.
*/
Two a = new Two();
}
}
Per comment of #cricket_007, a better solution for the public class() method would be:
public class One {
String asd;
public One(){
asd = "2d6";
}
}
This way, when an One object is made (One one = new One), it has a asd field with "2d6" already.
I am at a sticking point with an assignment I have been working on. It is a program that creates a Periodic Table of the Elements. To create each element, I need to take a file(a CSV), split it, and set each split part as an attribute of an object called "Element". Splitting each line of the file and reading it in has so far been no problem. However, I have been stuck on how to take each part and add it as the attribute of the Element object. Can anyone please shed some light on this?
Here is my code:
"Periodic Table" class:
public class PeriodicTable {
private String line;
private String[] parts;
private String filename = "file.csv";
public PeriodicTable() throws IOException {
}
public void readValues(String filename) throws IOException{
Scanner sc = new Scanner(new FileReader(filename));
ArrayList<Element> ar1 = new ArrayList<Element>();
while(sc.hasNextLine()){
line = sc.nextLine();
parts = line.split(",");
Element el = new Element();
el.setChemicalName(parts[0]);
el.setAtomNum(parts[1]);
el.setMeltPoint(parts[3]);
el.setBoilPoint(parts[4]);
el.setDensity(parts[5]);
el.setAtomWeight(parts[6]);
// System.out.println(el.getChemicalName());
//System.out.println(el.getAtomWeight());
ar1.add(el);
}
System.out.println(ar1);
}
}
The "Element" class
public class Element {
private String chemicalName;
private String atomNum;
private String boilPoint;
private String meltPoint;
private String density;
private String atomWeight;
public Element() throws IOException{
}
/**
* #return the chemicalName
*/
public String getChemicalName() {
return chemicalName;
}
/**
* #param chemicalName the chemicalName to set
*/
public void setChemicalName(String chemicalName) {
this.chemicalName = chemicalName;
}
/**
* #return the atomNum
*/
public String getAtomNum() {
return atomNum;
}
/**
* #param atomNum the atomNum to set
*/
public void setAtomNum(String atomNum) {
this.atomNum = atomNum;
}
/**
* #return the boilPoint
*/
public String getBoilPoint() {
return boilPoint;
}
/**
* #param parts2 the boilPoint to set
*/
public void setBoilPoint(String parts2) {
this.boilPoint = parts2;
}
/**
* #return the meltPoint
*/
public String getMeltPoint() {
return meltPoint;
}
/**
* #param parts2 the meltPoint to set
*/
public void setMeltPoint(String parts2) {
this.meltPoint = parts2;
}
/**
* #return the density
*/
public String getDensity() {
return density;
}
/**
* #param density the density to set
*/
public void setDensity(String density) {
this.density = density;
}
/**
* #return the atomWeight
*/
public String getAtomWeight() {
return atomWeight;
}
/**
* #param input the atomWeight to set
*/
public void setAtomWeight(String input) {
this.atomWeight = input;
}
#Override
public String toString(){
return chemicalName;
}
}
Because the String.split() method uses an array to hold each part that is split, I tried to set the values to each attribute using a reference to the index in the parts array that holds the value, for example....
el.setChemicalName(parts[0]));
However, this doesn't seem to be working - when I try to print out the value of that variable, I usually get a null value.
I recommend you to use OpenCSV to make your life easier.
Your code work fine, but please
Check the variable filename , maybe you pass an wrong value when call the method readValues.
Are you sure the token separator of your CSV file is a comma ",". If you're using an office application (Excel in MS Office or Calc in Libre/Open Office) if you don't especify the token the default separator is a semicolon ";".
In method String.split it's better use parts = line.split("\\,"); because split use and regular expressions to make the separation in String array.
I'm getting a null pointer exception in one of my java classes.
I have been looking at this thing for ever now and I could use another set of eyes.
I have 2 Class files:
FileParser:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package photouploader;
import java.nio.*;
import java.io.*;
import java.util.*;
/**
*
* #author 10339
*/
public class fileParser {
/*var Decleration*/
private File dir;
private File [] Files;
public String [] Filernames;
/*Accessor Methods*/
public File getDir(){
return dir;
}
public void SetDir(File directory){
dir=directory;
}
public File[] getFiles(){
return Files;
}
public void setFiles(File [] matches){
Files=matches.clone();
}
public String [] getFilenames(){
return Filernames;
}
public void setFilenames(){
for(int i=0;i<Files.length;i++){
System.out.println(Files[i].toString());
System.out.println(Files[i].toString().substring(Files[i].toString().lastIndexOf("\\")+1));
Filernames[i]=Files[i].toString().substring(Files[i].toString().lastIndexOf("\\")+1);
}
}
}
Photo Uploader Class file/Main:
package photouploader;
import javax.swing.*;
import java.io.*;
/**
*
* #author 10339
*/
public class PhotoUploader {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFileChooser j = new JFileChooser();
j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = j.showOpenDialog(null);
if(returnVal == javax.swing.JFileChooser.APPROVE_OPTION){
File dir = j.getSelectedFile();
File[] matches = dir.listFiles(new FilenameFilter()
{
public boolean accept(File dir,String name)
{
return name.endsWith(".png")||name.endsWith(".jpg");
}
});
if(matches.length==0){
JOptionPane.showMessageDialog(null,"you have not chosen "
+ "a valid directory");
}
else{
fileParser FP = new fileParser();
FP.setFiles(matches);
FP.setFilenames();
/* Form FJP = new Form();
FJP.setVisible(true);*/
NewJFrame JP = new NewJFrame();
JP.setVisible(true);
JP.update_Upload_list(FP.getFilenames());
}
}
else{
JOptionPane.showMessageDialog(null,"you have not chosen "
+ "a valid directory");
}
}
}
I have attempted to just place data # position 0 of Filternames and it is still freaking out.
Have I missed something?
do I need to Declare the size of the Array before use?
Thanks,
Scorliss
You have to allocate memory for the array elements:
Filernames = new String[Files.length];
EDIT:
So declaration is not enough?
No. In a nutshell, when you declare the variable, you are just saying that "this variable will point to an array". In that moment, it doesn't hold any array at all. You have to asign an array to the variable in order to use it. You can read a bit more about array basics here.
The String[] Filernames, has not been declared like: String[] Filernames = new String[5];
(or = new String[some length];) Also you should follow Java code conventions such as only class names begin with a capital letter, and variable names start with lower case letters.
I was wondering, given the following JSON, how I can produce a ResultSet instance, which carry Query valued ppb?
package jsontest;
import com.google.gson.Gson;
/**
*
* #author yccheok
*/
public class Main {
public static class ResultSet {
public String Query;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
final String s = "{\"ResultSet\":{\"Query\":\"ppb\"}}";
System.out.println(s);
Gson gson = new Gson();
ResultSet resultSet = gson.fromJson(s, ResultSet.class);
// {}
System.out.println(gson.toJson(resultSet));
// null?
System.out.println(resultSet.Query);
}
}
Currently, here is what I get :
{"ResultSet":{"Query":"ppb"}}
{}
null
Without modified the String, how can I get a correct Java object?
Try first to construct a new object, call gson.toJson(object), and see the result.
I don't have gson, but jackson (another object-to-json mapper) prints this:
{"Query":"ppb"}
So, you don't include the class name. Actually, the gson user guide gives an example showing exactly this. Look at the BagOfPrimitives.
(And a final note - in Java, the accepted practice is that variables are lowercase - i.e. query rather than Query)
Update If you really can't change the json input, you can mirror the structure this way:
public static class Holder {
public ResultSet ResultSet;
}
public static class ResultSet {
public String Query;
}
(and then use Holder h = gson.fromJson(s, Holder.class);)